async and await on array reduce function

You, JavaScriptES6
Back

Array reduce is a very useful function to reduce the array into a single value, without async await syntax or Promise.resolve() wrappers it won't wait for asynchronous requests so here is how to make the array reduce function to use async await to make an async requests.

const times = await array.reduce(async (accuP, current) => {
    let accum = await accuP;
    // Your async request goes here
    const value = await fetch(url);
    return accum;
}, []);
© Heshan Wanigasooriya.RSS

🍪 This site does not track you.