diff --git a/lib/then.js b/lib/then.js index 94e7c80e..731454a8 100644 --- a/lib/then.js +++ b/lib/then.js @@ -1,16 +1,21 @@ -var Gun = Gun || require('../gun'); +var Gun = (typeof window !== "undefined")? window.Gun : require('../gun'); +// Returns a gun reference in a promise and then calls a callback if specified Gun.chain.promise = function(cb) { var gun = this, cb = cb || function(ctx) { return ctx }; return (new Promise(function(res, rej) { - gun.val(function(data, key){ - res({put: data, get: key, gun: this}); + gun.once(function(data, key){ + res({put: data, get: key, gun: this}); // gun reference is returned by promise }); - })).then(cb); + })).then(cb); //calling callback with resolved data }; -Gun.chain.then = function(cb) { - return this.promise(function(res){ - return cb? cb(res.put) : res.put; - }); -}; \ No newline at end of file +// Returns a promise for the data, key of the gun call +Gun.chain.then = function() { + var gun = this; + return (new Promise((res, rej)=>{ + gun.once(function (data, key) { + res(data, key); //call resolve when data is returned + }) + })) +};