1var p1 = Promise.resolve(3);
2var p2 = 1337;
3var p3 = new Promise((resolve, reject) => {
4 setTimeout(resolve, 100, "foo");
5});
6
7Promise.all([p1, p2, p3]).then(values => {
8 console.log(values); // [3, 1337, "foo"]
9});
1 try {
2 const [res1, res2, res3] = await Promise.all([
3 Promise1,
4 Promise2,
5 Promise3,
6 ]);
7 } catch (err) {
8 console.error(err);
9 }
10
1Promise.all([Promise1, Promise2, Promise3])
2 .then(result) => {
3 console.log(result)
4 })
5 .catch(error => console.log(`Error in promises ${error}`))
1const promise1 = Promise.resolve(3);
2const promise2 = 42;
3const promise3 = new Promise(function(resolve, reject) {
4 setTimeout(resolve, 100, 'foo');
5});
6
7Promise.all([promise1, promise2, promise3]).then(function(values) {
8 console.log(values);
9});
10// expected output: Array [3, 42, "foo"]
11
1// PARALLEL PROMISES
2async function fetchMoviesAndCategories() {
3 const [moviesResponse, categoriesResponse] = await Promise.all([
4 fetch('/movies'),
5 fetch('/categories')
6 ]);
7
8 const movies = await moviesResponse.json();
9 const categories = await categoriesResponse.json();
10
11 return [movies, categories];
12}
1const durations = [1000, 2000, 3000]
2
3promises = durations.map((duration) => {
4 return timeOut(duration).catch(e => e) // Handling the error for each promise.
5})
6
7Promise.all(promises)
8 .then(response => console.log(response)) // ["Completed in 1000", "Rejected in 2000", "Completed in 3000"]
9 .catch(error => console.log(`Error in executing ${error}`))
10view raw