Skip to content

Latest commit

 

History

History
343 lines (230 loc) · 8.04 KB

README-async.md

File metadata and controls

343 lines (230 loc) · 8.04 KB

Async

Control flow ala ES7 async/await with async.js (v2) signatures


Table of Contents


Motivation

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.

Addition to the async library signatures / promise pooling

  • 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.

API

eachLimit(arr, concurrency, thunk [, thisobj]) : Promise

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);
  });

})();

eachOfLimit(dict, concurrency, thunk [, thisobj]) : Promise

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);
  });

})();

each(arr, thunk [, thisobj]) : Promise

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);
  });

})();

eachOf(dict, thunk [, thisobj]) : Promise

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);
  });

})();

eachSeries(arr, thunk [, thisobj]) : Function

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);
  });

})();

eachOfSeries(dict, thunk [, thisobj]) : Function

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);
  });

})();

retryUntil(thunk, timeout, delay, [, failure]) : Function

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');

})();

sleep(delay) : Promise

setTimeout as a promise.

const sleep = require('nyks/async/sleep');

(async function() {

  // this is now
  await sleep(2000);
  // this is now + 2 secondes

})();

timeout(delay, [, reason]) : Promise

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'
})();

setImmediate(fn) : Function

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...

})();

queue(thunk, concurrency) : Object

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(thunk, concurrency) : Function

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
})();

Tests

nyks/async is tested against async test suite (of course)


TODO


Credits


Alternatives / relatives

  • 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

Shoutbox, keywords, SEO love

async/await, co, nyks/async, promise, Promises, yield, async, queue, map, throttle, "Let's have a beer & talk in Paris"