Last active
February 23, 2017 18:17
-
-
Save bayleedev/601b410875c5ad03f41d367b89f661bf to your computer and use it in GitHub Desktop.
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
| const urls = [ | |
| 'https://google.com', | |
| 'http://blainesch.com', | |
| 'https://github.com', | |
| ] | |
| let follower = Promise.resolve() | |
| for (let currentUrl of urls) { | |
| follower = follower.then(() => { | |
| return new Promise((resolve) => { | |
| console.log('processing', currentUrl) | |
| setTimeout(resolve, 1000) | |
| }) | |
| }) | |
| } |
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
| const urls = [ | |
| 'https://google.com', | |
| 'http://blainesch.com', | |
| 'https://github.com', | |
| ] | |
| const process = (urls) => { | |
| const currentUrl = urls.shift() | |
| return new Promise((resolve) => { | |
| console.log('processing', currentUrl) | |
| setTimeout(resolve, 1000) | |
| }).then(() => { | |
| if (urls.length > 0) { | |
| return process(urls) | |
| } | |
| }) | |
| } | |
| process(urls.slice(0)).then(() => { | |
| console.log('done') | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Abstracted timeout to a function.