then.js 725 B

123456789101112131415161718192021
  1. var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
  2. // Returns a gun reference in a promise and then calls a callback if specified
  3. Gun.chain.promise = function(cb) {
  4. var gun = this, cb = cb || function(ctx) { return ctx };
  5. return (new Promise(function(res, rej) {
  6. gun.once(function(data, key){
  7. res({put: data, get: key, gun: this}); // gun reference is returned by promise
  8. });
  9. })).then(cb); //calling callback with resolved data
  10. };
  11. // Returns a promise for the data, key of the gun call
  12. Gun.chain.then = function() {
  13. var gun = this;
  14. return (new Promise((res, rej)=>{
  15. gun.once(function (data, key) {
  16. res(data, key); //call resolve when data is returned
  17. })
  18. }))
  19. };