diff options
Diffstat (limited to 'test/es2017/awaitableFunctions.js')
| -rw-r--r-- | test/es2017/awaitableFunctions.js | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js new file mode 100644 index 0000000..ce36867 --- /dev/null +++ b/test/es2017/awaitableFunctions.js @@ -0,0 +1,59 @@ +var async = require('../../lib'); +const {expect} = require('chai'); +const assert = require('assert'); + + +module.exports = function () { + async function asyncIdentity(val) { + var res = await Promise.resolve(val); + return res; + } + + const input = [1, 2, 3]; + const inputObj = {a: 1, b: 2, c: 3}; + + it('should asyncify async functions', (done) => { + async.asyncify(asyncIdentity)(42, (err, val) => { + assert(val === 42); + done(err); + }) + }); + + + /* + * Collections + */ + + it('should return a Promise: each', async () => { + const calls = [] + await async.each(input, async val => { calls.push(val) }); + expect(calls).to.eql([1, 2, 3]) + expect(async.each(input, asyncIdentity) instanceof Promise).to.equal(true) + }); + it('should return a Promise: eachSeries', async () => { + const calls = [] + await async.eachSeries(input, async val => { calls.push(val) }); + expect(calls).to.eql([1, 2, 3]) + }); + it('should return a Promise: eachLimit', async () => { + const calls = [] + await async.eachLimit(input, 1, async val => { calls.push(val) }); + expect(calls).to.eql([1, 2, 3]) + }); + + it('should return a Promise: eachOf', async () => { + const calls = [] + await async.eachOf(inputObj, async (...args) => { calls.push(args) }); + expect(calls).to.eql([[1, 'a'], [2, 'b'], [3, 'c']]) + }); + it('should return a Promise: eachOfSeries', async () => { + const calls = [] + await async.eachOfSeries(inputObj, async (...args) => { calls.push(args) }); + expect(calls).to.eql([[1, 'a'], [2, 'b'], [3, 'c']]) + }); + it('should return a Promise: eachOfLimit', async () => { + const calls = [] + await async.eachOfLimit(inputObj, 1, async (...args) => { calls.push(args) }); + expect(calls).to.eql([[1, 'a'], [2, 'b'], [3, 'c']]) + }); +}; |
