Control flow ala ES7 async/await with async.js (v2) signatures
nyks/async provide javascript async/await equivalent signatures of the excellent async workflow library.
Module are exported in standard commonJS module format and written in strict format. (no transpilation required nor used). Use browserify if you need nyks/async modules in a browser environnement.
nyks/async is not a wrapper on async, but rather leverages the full potential of native async/await & promises contract. Code tend to be small & very efficient (far more simplier than using callbacks), just give nyks/async/queue.js a look.
- Async functions cannot use arrow function binding style, yet it might be usefull to bind nyks/async closure, therefore, you can use an extra optional args to all signature to set async function binding context. (i.e. as in native .forEach )
- Per design, it's easy to "throttle" a function that return a Promise ; checkout the "throttle" API for a way to make an ultra simple http request pooling.
- async logic allow async/each to iterate through array AND objects. Per design sanify, nyks/async does not. Use each/eachLimit/eachSeries for arrays, eachOf/eachOfLimit/eachOfSeries for collections.
Applies the function 'thunk' to each item in 'arr', in parallel, with a limit async operations of 'concurrency' at a time. The 'thunk' is called with an item from the Array 'arr'.
const eachLimit = require('nyks/async/eachLimit');
(async function() {
var stuffs = [1, 2, 3, 5, 6];
await eachLimit(stuffs, 2, async function(id) {
await dootherStuffs(id);
});
})();
Applies the function 'thunk' to each item of 'dict', in parallel, with a limit async operations of 'concurrency' at a time. The 'thunk' is called with an item from the Object 'dict'.
const eachOfLimit = require('nyks/async/eachOfLimit');
(async function() {
var stuffs = {
'number' : 1,
'number' : 2,
'number' : 3,
'number' : 4
};
await eachOfLimit(stuffs, 2, async function(id, key) {
await dootherStuffs(id);
});
})();
Like eachLimit with a concurrency of arr.length.
const each = require('nyks/async/each');
(async function() {
var stuffs = [1, 2, 3, 5, 6];
await each(stuffs, async function(id) {
await dootherStuffs(id);
});
})();
Like eachOfLimit with a concurrency of dict.length.
const eachOf = require('nyks/async/eachOf');
(async function() {
var stuffs = {
'number' : 1,
'number' : 2,
'number' : 3,
'number' : 4
};
await eachOf(stuffs, async function(id, key) {
await dootherStuffs(id);
});
})();
Like eachLimit with a concurrency of 1.
const eachSeries = require('nyks/async/eachSeries');
(async function() {
var stuffs = [1, 2, 3, 5, 6];
await eachSeries(stuffs, async function(id) {
await dootherStuffs(id);
});
})();
Like eachOfLimit with a concurrency of 1.
const eachOfSeries = require('nyks/async/eachOfSeries');
(async function() {
var stuffs = {
'number' : 1,
'number' : 2,
'number' : 3,
'number' : 4
};
await eachOfSeries(stuffs, async function(id, key) {
await dootherStuffs(id);
});
})();
Run thunk until it resolves a truthy value, until timeout is reached. Wait delay between each attempt. On timeout, reject with a generic "Timeout" error message (or the specified failure, if any).
const retryUntil = require('nyks/async/retryUntil');
(async function() {
await tryUntil(async () => {
return net.connect(...)
}, 60 * 1000, 1000, 'Could not connect to remote socket');
})();
setTimeout as a promise.
const sleep = require('nyks/async/sleep');
(async function() {
// this is now
await sleep(2000);
// this is now + 2 secondes
})();
return a Promise that reject after delay (with reason = 'timeout')
const timeout = require('nyks/async/timeout');
(async function() {
await timeout(1000); // this will throw with message 'timeout'
})();
Call a function in javascript next tick (using setImmediate API, or timeout 0 pollyfill)
const setImmediate = require('nyks/async/setImmediate');
(async function() {
// this is now
await setImmediate();
// this is still now...
})();
Return a QueueObject you can push task into. Wait for thunk to process task (wait for worker, if needed).
const fetch = require('node-fetch');
const queue = require('nyks/async/queue');
var q = queue(fetch, 1); //let's be nice
(async function() {
await q.push("http://example.com/stuff.json");
})();
(async function() {
await q.push("http://example.com/otherstuff.json"); //will wait for stuff to be retrieved
})();
Throttle any function that return a promise, sugar syntax helper for nyks/async/queue.
const fetch = require('node-fetch');
const throttle = require('nyks/async/throttle');
fetch = throttle(fetch, 1); //make fetch behave nicely
(async function() {
await fetch("http://example.com/stuff.json");
})();
(async function() {
await fetch("http://example.com/otherstuff.json"); //will wait for stuff.json to be retrieved
})();
nyks/async is tested against async test suite (of course)
- Get rich or die tryin'
- write a working nyks/async/cargo (see the challenge on stackoverflow)
- koa-async; a clever Promisify wrapper on top of async (but not leveraging the full potential of ES7 async/await capabilities)
- caolan/async/asyncify.js; goes the same as koa-async.
- es6-promise-pool; equivalent to nyks/async/queue, with a different API
async/await, co, nyks/async, promise, Promises, yield, async, queue, map, throttle, "Let's have a beer & talk in Paris"