mirror of
https://github.com/amark/gun.git
synced 2025-06-03 20:56:43 +00:00
proposed lib/then
This commit is contained in:
parent
6ea408700e
commit
e0c93228ee
23
lib/then.js
23
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;
|
||||
});
|
||||
};
|
||||
// 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
|
||||
})
|
||||
}))
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user