Skip to content Skip to sidebar Skip to footer

Sequential Promises Using Async/await In Vanilla Javascript

I would like to know how to solve running an array of promises using async, in sequential order, but specifically how to pass the resolved value from one function to the next. All

Solution 1:

You can create a pipeline like this:

let fnArray  = [foo,  bar, baz];
let lastResult;
for(let i = 0; i < fnArray.length; i++) {
     lastResult = await fnArray[i](lastResult);
}

Solution 2:

Wrap them in a .then()

foo()
    .then(valueFoo => bar(valueFoo))
    .then(valueBar => AnotherFunc(valueBar))
    .catch(e => { console.log(e)})

Post a Comment for "Sequential Promises Using Async/await In Vanilla Javascript"