Skip to content

Instantly share code, notes, and snippets.

@syzer
Created September 8, 2018 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syzer/448e669a7598e2140c9cce8c7c330403 to your computer and use it in GitHub Desktop.
Save syzer/448e669a7598e2140c9cce8c7c330403 to your computer and use it in GitHub Desktop.
Example of async requests in series
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