Skip to content

Instantly share code, notes, and snippets.

@romulomourao
Created July 28, 2017 01:57
Show Gist options
  • Save romulomourao/974d381ce1799f43e741509191e7e602 to your computer and use it in GitHub Desktop.
Save romulomourao/974d381ce1799f43e741509191e7e602 to your computer and use it in GitHub Desktop.
Simple snippet to demonstrate behavior of Promise.all
// Timeout
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Resolve promise 1");
}, 3000);
});
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Resolve promise 2");
}, 1000);
});
const p3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Resolve promise 3");
}, 4000);
});
Promise.all([p1,p2,p3])
.then((res) => {
console.log(res);
});
// Requisição
const r1 = fetch('https://jsonplaceholder.typicode.com/posts').then(res => res.json());
const r2 = fetch('https://jsonplaceholder.typicode.com/users').then(res => res.json());
const r3 = fetch('https://jsonplaceholder.typicode.com/comments').then(res => res.json());
Promise.all([r1,r2,r3])
.then((res) => {
console.log(res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment