What is run-series?
The run-series npm package is a utility that allows you to run an array of functions in series, each one running once the previous function has completed. If any functions in the series pass an error to its callback, no more functions are run, and the main callback is immediately called with the error. This package is particularly useful for managing sequences of asynchronous tasks in a clean and straightforward manner.
What are run-series's main functionalities?
Running tasks in series
This feature allows you to run multiple tasks one after another. Each task is a function that takes a callback as its only argument. Once a task completes, it calls the callback, optionally passing an error and results. If a task passes an error, the series is stopped, and the final callback is called with the error. Otherwise, after all tasks have completed, the final callback is called with an array of results.
const series = require('run-series');
series([
function(callback) {
// Task 1
callback(null, 'result of task 1');
},
function(callback) {
// Task 2
callback(null, 'result of task 2');
}
], function(err, results) {
if (err) throw err;
console.log(results); // ['result of task 1', 'result of task 2']
});
Other packages similar to run-series
async
The 'async' package provides a wide range of functions for working with asynchronous JavaScript, including a 'series' method that behaves similarly to run-series. However, 'async' offers much more functionality beyond just running tasks in series, such as parallel execution, waterfall flows, and more, making it a more versatile choice for complex asynchronous control flow.
neo-async
Neo-async is a drop-in replacement for the 'async' library, designed to be faster and more lightweight. It offers a 'series' function similar to that of run-series and async, but with performance improvements and additional features for handling asynchronous operations, making it a good choice for performance-critical applications that require complex flow control.
run-series
Run an array of functions in series
install
npm install run-series
usage
series(tasks, [callback])
Run the functions in the tasks
array in series, each one running once the previous
function has completed. If any functions in the series pass an error to its callback, no
more functions are run, and callback
is immediately called with the value of the error.
Otherwise, callback
receives an array of results when tasks
have completed.
arguments
tasks
- An array containing functions to run, each function is passed a
callback(err, result)
which it must call on completion with an error err
(which can
be null
) and an optional result value.callback(err, results)
- An optional callback to run once all the functions have
completed. This function gets a results array containing all the result arguments passed
to the task callbacks.
example
var series = require('run-series')
series([
function (callback) {
callback(null, 'one')
},
function (callback) {
callback(null, 'two')
}
],
function (err, results) {
})
series.waterfall(tasks, [callback])
Runs the tasks
array of functions in series, each passing their results to the next in
the array. However, if any of the tasks
pass an error to their own callback, the next
function is not executed, and the main callback
is immediately called with the error.
arguments
tasks
- An array of functions to run, each function is passed a
callback(err, result1, result2, ...)
it must call on completion. The first argument is
an error (which can be null
) and any further arguments will be passed as arguments in
order to the next task.callback(err, [results])
- An optional callback to run once all the functions have
completed. This will be passed the results of the last task's callback.
example
var series = require('run-series')
series.waterfall([
function (callback) {
callback(null, 'one', 'two')
},
function (arg1, arg2, callback) {
callback(null, 'three')
},
function (arg1, callback) {
callback(null, 'done', 'wohoo')
}
], function (err, result1, result2) {
})
This module is basically equavalent to
async.series
and
async.waterfall
, but it's
handy to just have the functions you need instead of the kitchen sink. Modularity!
Especially handy if you're serving to the browser and need to reduce your javascript
bundle size.
Works great in the browser with browserify!
license
MIT. Copyright (c) Feross Aboukhadijeh.