filterLimit.mjs


import mapLimit from './mapLimit.mjs'
import assert from 'nanoassert'

/**
 * Returns a new array of all the values in iterable which pass an asynchronous truth test.
 *
 * The calls to `iteratee` will perform in parallel, up to the concurrency limit, but the results array will be
 * in the same order than the original.
 *
 * If any of the calls to iteratee throws an exception the returned promised will be rejected and the remaining
 * pending tasks will be cancelled.
 *
 * @param {Iterable} iterable An iterable object.
 * @param {Function} iteratee A function that will be called with each member of `iterable`. It will receive
 * three arguments:
 *   * `value`: The current value to process
 *   * `index`: The index in the iterable. Will start from 0.
 *   * `iterable`: The iterable on which the operation is being performed.
 * @param {number} concurrency The number of times iteratee can be called concurrently.
 * @returns {Promise} A promise that will be resolved with an array containing all the values that passed
 * the truth test. This promise will be rejected if any of the `iteratee` calls throws an exception.
 * @example
 * import { filterLimit, asyncRoot, sleep } from 'modern-async'
 *
 * asyncRoot(async () => {
 *   const array = [1, 2, 3]
 *   const result = await filterLimit(array, async (v) => {
 *     // these calls will be performed in parallel with a maximum of 2
 *     // concurrent calls
 *     await sleep(10) // waits 10ms
 *     return v % 2 === 1
 *   }, 2)
 *   console.log(result) // prints [1, 3]
 *   // total processing time should be ~ 20ms
 * })
 */
async function filterLimit (iterable, iteratee, concurrency) {
  assert(typeof iteratee === 'function', 'iteratee must be a function')
  return (await mapLimit(iterable, async (v, i, t) => {
    return [v, await iteratee(v, i, t)]
  }, concurrency)).filter(([v, t]) => t).map(([v, t]) => v)
}

export default filterLimit