using promise.all code example
Example 1: javascript promise.all
try {
const [res1, res2, res3] = await Promise.all([
Promise1,
Promise2,
Promise3,
]);
} catch (err) {
console.error(err);
}
Example 2: javascript promise.all
async function fetchMoviesAndCategories() {
const [moviesResponse, categoriesResponse] = await Promise.all([
fetch('/movies'),
fetch('/categories')
]);
const movies = await moviesResponse.json();
const categories = await categoriesResponse.json();
return [movies, categories];
}
Example 3: promise all in promise all
var arr = [
{subarr: [1,2,3]},
{subarr: [4,5,6]},
{subarr: [7,8,9]}
];
function processAsync(n) {
return new Promise(function(resolve) {
setTimeout(
function() { resolve(n * n); },
Math.random() * 1e3
);
});
}
Promise.all(arr.map(function(entity){
return Promise.all(entity.subarr.map(function(item){
return processAsync(item);
}));
})).then(function(data) {
console.log(data);
});