diff --git a/lib/promise.js b/lib/promise.js deleted file mode 100644 index 6fc2f7c1..00000000 --- a/lib/promise.js +++ /dev/null @@ -1,10 +0,0 @@ -var Gun = Gun || require('../gun'); - -Gun.chain.promise = function(cb) { - var gun = this, cb = cb || function(ctx) { return ctx }; - return (new Promise(function(res, rej) { - gun.val(function(node, key) { - res({val: node, key: key, gun: gun}); - }); - })).then(cb); -}; diff --git a/lib/then.js b/lib/then.js new file mode 100644 index 00000000..94e7c80e --- /dev/null +++ b/lib/then.js @@ -0,0 +1,16 @@ +var Gun = Gun || require('../gun'); + +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}); + }); + })).then(cb); +}; + +Gun.chain.then = function(cb) { + return this.promise(function(res){ + return cb? cb(res.put) : res.put; + }); +}; \ No newline at end of file diff --git a/package.json b/package.json index 1346a042..9c22d235 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gun", - "version": "0.8.5", + "version": "0.8.6", "description": "Graph engine", "main": "index.js", "browser": "gun.min.js", diff --git a/promise-test.js b/promise-test.js deleted file mode 100644 index 6c98e949..00000000 --- a/promise-test.js +++ /dev/null @@ -1,44 +0,0 @@ -var Gun = require('./gun'); -require('./lib/promise'); - -var gun = new Gun(); - -/* prep */ -var mark = gun.get('mark').put({ - name: 'mark' -}) -var cat = gun.get('cat').put({ - name: 'sylvester' -}); -mark.get('boss').put(cat); -cat.get('slave').put(mark); - -/* async/await syntax */ -async function getField(field) { - var node = await gun.get(field).promise(); - console.log({1: node.val}); - return node; -}; - -setTimeout(async () => { - var mark = await getField('mark'); - console.log({2: mark.val}); -}, 100); - -/* chained thens */ -setTimeout(() => { - gun.get('mark') - .promise(ctx => { - console.log({a: ctx.val}); - return mark.get('boss').promise(); - }) - .then(cat => { - console.log({b: cat.val}); - return cat.gun.get('slave').promise(); - }) - .then(mark => { - console.log({c: mark.val}); - process.exit(); - }); -}, 200); -