Created
October 20, 2017 09:16
-
-
Save muhammadfaizan/1461dc6f4d8f682af48cc5f0e7f5096b to your computer and use it in GitHub Desktop.
A gist to loop promise
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
let tasks = [ /* ... */ ] | |
let promise = tasks.reduce((prev, task) => { | |
return prev.then(() => { | |
return task(); | |
}); | |
}, Promise.resolve()); | |
promise.then(() => { | |
//All tasks completed | |
}); |
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
let tasks = [ /* ... */ ] | |
let promise = Promise.resolve(); | |
tasks.forEach(task => { | |
promise = promise.then(() => { | |
return task(); | |
}); | |
}); | |
promise.then(() => { | |
//All tasks completed | |
}); |
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
function spiderLinks(currentUrl, body, nesting) { | |
let promise = Promise.resolve(); | |
if(nesting === 0) { | |
return promise; | |
} | |
const links = utilities.getPageLinks(currentUrl, body); | |
links.forEach(link => { | |
promise = promise.then(() => spider(link, nesting - 1)); | |
}); | |
return promise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment