Created
September 8, 2018 15:27
-
-
Save syzer/448e669a7598e2140c9cce8c7c330403 to your computer and use it in GitHub Desktop.
Example of async requests in series
This file contains 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
const sleep = s => | |
new Promise(resolve => | |
setTimeout(resolve, s * 1000)) | |
const fetch = url => | |
new Promise(resolve => | |
setTimeout(() => resolve(url), 1.5 * 1000)) | |
// Promise.all([ | |
// 'a', | |
// 'b', | |
// sleep(2), | |
// ]).then(result => { | |
// console.log(result) | |
// }) | |
fetch('first url') | |
.then((result) => { | |
console.log(result, '<= first finished with this url') | |
return fetch('second url') | |
}) | |
.then(async (resultOf2ndFetch) => { | |
console.log('result of 2nd fetch: ', resultOf2ndFetch) | |
console.log('hey i wait here: =====') | |
await sleep(2) | |
console.log(' hey i finished waiting here :)') | |
return fetch('last url is here' + resultOf2ndFetch) | |
}) | |
.then(result => { | |
console.log('6 seconds passed, and heres result', result) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment