Created
December 10, 2018 16:55
-
-
Save bakoushin/9a4c9d30ea5285854a7a173994d28d43 to your computer and use it in GitHub Desktop.
Start downloading files simultaneously, but display them in order
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 files = [fetch('file1'), fetch('file2'), fetch('file3')]; | |
let chain = Promise.resolve(); | |
for (const file of files) { | |
chain = chain | |
.then(() => { | |
return file; | |
}) | |
.then(file => console.log(file)); | |
} | |
function fetch(file) { | |
const delay = Math.random() * 3000; | |
return new Promise(resolve => { | |
setTimeout(() => { | |
console.log(`${file} received`); | |
resolve(file); | |
}, delay); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment