foreign

3.1.2 • Public • Published

foreign

Asnyc map that actualy works. Parallel, Series, Sync and Async

Usage

const foreign = require('foreign');

Parallel

const items = [1, 2, 3];

foreign.parallel(
    (item, callback) => {
        somethingAsync(item, callback);
    },
    items,
    (error, result) => {},
);

Series

const items = [1, 2, 3];

foreign.series(
    (item, callback) => {
        somethingAsync(item, callback);
    },
    items,
    (error, result) => {},
);

SeriesPromise

const items = [1, 2, 3];

const result = foreign.series(async (item) => someAsyncFunction(item), items);

SeriesAll

const items = [1, 2, 3];

foreign.seriesAll(
    (item, callback) => {
        somethingAsync(item, callback);
    },
    items,
    (error, result) => {
        // 'error' will always be a either array or object of errors
        // 'result' will always be a either array or object of results
        // depending on the type `items` is.
        //
        // NOTE: `error.length` is not a reliable indicator of number of errors
        //
        // All items will be processed in order even if
        // some return errors. `foreign.series` and `foreign.seriesPromise` will return
        // on first error

        // example error checking:
        if (error) {
            const itemsThatFailed = Object.keys(error);
        }
    },
);

Multiple return values

const items = [1, 2, 3];

foreign.series(
    (item, callback) => {
        callback(null, item, 'foo');
    },
    items,
    (error, results) => {
        console.log(results); // [[1, 'foo'], [2, 'foo'], [3, 'foo']]
    },
);

Package Sidebar

Install

npm i foreign

Weekly Downloads

61

Version

3.1.2

License

MIT

Unpacked Size

19 kB

Total Files

13

Last publish

Collaborators

  • mauricebutler