Merge pull request #803 from Dletta/then

proposed lib/then
This commit is contained in:
Mark Nadal 2019-08-29 13:43:58 -07:00 committed by GitHub
commit d148f46d30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,16 +1,21 @@
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.once(function(data, key){
res({put: data, get: key, gun: this});
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;
});
};
// 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
})
}))
};