Last active
May 31, 2018 17:35
-
-
Save crates/bbcd2686a4648b8371e1f7759882a4b5 to your computer and use it in GitHub Desktop.
Parallel, asynchronous AJAX calls in ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parallel = async (...items) => { | |
const temp = []; | |
for (const item of items) { | |
temp.push(await item); | |
} | |
return temp; | |
}; | |
const finalResult = await parallel(someResult(), anotherResult()); | |
//or | |
//const [result1, result2] = await parallel(someResult(), anotherResult()); | |
// Alternative Implementation: | |
async function stepVerifyIdentity(nextState, replace, callback) { | |
const [ val1, val2, val3 ] = await Promise.all([ | |
promise1('Param1'), | |
promise2('Param2'), | |
promise3('Param3') | |
]) | |
if (!val1 && (!val2 || !val3)) { | |
console.log('Do something') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sourced from:
https://stackoverflow.com/questions/35612428/call-async-await-functions-in-parallel
https://stackoverflow.com/questions/39371954/awaiting-several-promises-in-one-async-function
https://stackoverflow.com/questions/24193595/slowdown-due-to-non-parallel-awaiting-of-promises-in-async-generators