Skip to content

Async Map

Sykander edited this page Dec 27, 2019 · 8 revisions

Async Map

Try iterable-async on RunKit

Docblock

/**
 * Async Map
 * Map an iterable object asynchronously and resolve when all callbacks are resolved
 * @async
 * @param {Function} callback - callback(currentValue, index, array)
 * @param {Object} [thisArg] - must be iterable
 * @return {Array}
 * @throws {TypeError}
 */
async function asyncMap(callback, [thisArg]) {...}

Examples

As a function

const { asyncFilter } = require('iterable-async'),
  array = [1, 2, 3];

const found = await asyncMap(async element => {
  return element + 2;
}, array);

console.log('Mapped new Array ', ...found);
// Mapped new Array 3, 4, 5

As a method

const { asyncFind } = require('iterable-async'),
  array = [1, 2, 3,];

array.asyncMap = asyncMap;

const found = await array.asyncMap(async element => {
    return element + 2;
});

console.log('Mapped new Array ', ...found);
// Mapped new Array 3, 4, 5
Clone this wiki locally