Skip to content

Instantly share code, notes, and snippets.

@bakoushin
Created December 10, 2018 16:55
Show Gist options
  • Save bakoushin/9a4c9d30ea5285854a7a173994d28d43 to your computer and use it in GitHub Desktop.
Save bakoushin/9a4c9d30ea5285854a7a173994d28d43 to your computer and use it in GitHub Desktop.
Start downloading files simultaneously, but display them in order
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