Created
July 28, 2017 01:57
-
-
Save romulomourao/974d381ce1799f43e741509191e7e602 to your computer and use it in GitHub Desktop.
Simple snippet to demonstrate behavior of Promise.all
This file contains hidden or 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
// 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