Finally async
/await
will be supported in all major browser soon except IE.
So now we can start writing more readable code with async
/await
but there is a catch. A lot of people use async await like this:
const userResponse = await fetchUserAsync();
const postsResponse = await fetchPostsAsync();
While this code is readable it has a problem, it runs the functions in series, it won't start fetching posts until the fetching of the user is finished. The solutions is simple, we need to fetch the resources in parallel.
So what I want to do is (in pseudo language):
fn task() {
result-1 = doAsync();
result-2 = doAsync();
result-n = doLongAsync();
// handle results together
combinedResult = handleResults(result-1, result-2);
lastResult = handleLastResult(result-n);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…