GPTScript / AiScript

A Minimal, Full-Stack, Tool-Assisted Language. Native to Browsers and Bun. Strictly & Strongly-Typed.
https://github.com/GPTScript/AiScript
Mozilla Public License 2.0
9 stars 1 forks source link

[std] Promise._map: run async functions sequentially and store results #14

Open wmerfalen opened 2 years ago

wmerfalen commented 2 years ago

Run fn on each element of arr, one at a time, and give back a sequentially ordered list of results.

// do something like `throw new Error('break');` to stop
Promise._map = async function (arr, fn) {
    let results = [];
    await arr.reduce(async function (promise, el, i) {
        await promise;
        let result = await fn(el, i, arr);
        results.push(result);
    }, Promise.resolve());
    return results;
};