What is now-and-later?
The now-and-later npm package is designed to execute functions in a specific order, allowing for both series and parallel execution with support for mapping and iterating over collections. It provides a way to manage and coordinate asynchronous tasks, making it easier to handle complex flows in Node.js applications.
What are now-and-later's main functionalities?
Map Series
Executes an iterator function on each item in an array, in series. Each iterator is called with the item, key, and a callback function. The results are collected and passed to the final callback.
const nal = require('now-and-later');
nal.mapSeries(['a', 'b', 'c'], function(value, key, cb) {
setTimeout(function() {
cb(null, value.toUpperCase());
}, 1000);
}, function(err, results) {
console.log(results); // ['A', 'B', 'C']
});
Map
Similar to mapSeries, but executes the iterator functions in parallel. The final callback is called once all iterators have completed.
const nal = require('now-and-later');
nal.map(['a', 'b', 'c'], function(value, key, cb) {
setTimeout(function() {
cb(null, value.toUpperCase());
}, 1000);
}, function(err, results) {
console.log(results); // ['A', 'B', 'C']
});
Map Values
Executes an iterator function on each property of an object, in parallel. The iterator is called with the value, key, and a callback function. The results are collected into an object and passed to the final callback.
const nal = require('now-and-later');
const obj = { a: 1, b: 2, c: 3 };
nal.mapValues(obj, function(value, key, cb) {
setTimeout(function() {
cb(null, value * 2);
}, 1000);
}, function(err, results) {
console.log(results); // { a: 2, b: 4, c: 6 }
});
Map Values Series
Works like mapValues but executes the iterator functions in series.
const nal = require('now-and-later');
const obj = { a: 1, b: 2, c: 3 };
nal.mapValuesSeries(obj, function(value, key, cb) {
setTimeout(function() {
cb(null, value * 2);
}, 1000);
}, function(err, results) {
console.log(results); // { a: 2, b: 4, c: 6 }
});
Other packages similar to now-and-later
async
The async package is a comprehensive collection of asynchronous control-flow utilities. It provides more than 70 functions including various forms of parallel, series, and race, as well as other utility functions. It is more widely used and has a larger community compared to now-and-later.
bluebird
Bluebird is a fully-featured promise library with a focus on innovative features and performance. It allows for conversion of callback-based functions to promises, which can then be used to manage asynchronous control flow. Bluebird offers more features related to promises compared to now-and-later.
q
Q is a promise library for JavaScript that provides a robust way of working with asynchronous operations. It allows you to create and manage promises, which can be used to organize asynchronous code more effectively. Q is more focused on promises than now-and-later, which uses callbacks.
now-and-later
Map over an array of values in parallel or series, passing each through the async iterator.
Optionally, specify lifecycle extension points for before the iterator runs, after completion,
or upon error.
Usage
var nal = require('now-and-later');
function iterator(value, cb){
cb(null, value * 2)
}
function create(value, key){
return { key: key, value: value };
}
function before(storage){
console.log('before iterator');
console.log('initial value: ', storage.value);
}
function after(result, storage){
console.log('after iterator');
console.log('initial value: ', storage.value);
console.log('result: ', result);
}
function error(err, storage){
console.log('afer error in iterator');
console.log('error: ', err);
}
nal.mapSeries([1, 2, 3], iterator {
create: create,
before: before,
after: after,
error: error
}, console.log);
nal.map({
fn1: fn1,
fn2: fn2
}, {
create: create,
before: before,
after: after,
error: error
}, console.log);