Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. function reductiveDownloadChain(files){
  2. return files.reduce((chain,currentFile) => {
  3. return chain.then(_ => download(currentFile));
  4. },Promise.resolve())
  5. }
  6.  
  7. reductiveDownloadChain(mockFiles)
  8. .then(_ => console.log('files were downloaded in reductive chain mode'))
  9.  
  10. // ----- output in console -----
  11.  
  12. // started downloading file 1 ...(immediately)
  13. // downloaded file 1 in 2000ms ...(after 2s)
  14.  
  15. // started downloading file 2 ...(after 2s)
  16. // downloaded file 2 in 4000ms ...(after 6s)
  17.  
  18. // started downloading file 3 ...(after 6s)
  19. // downloaded file 3 in 3000ms ...(after 9s)
  20.  
  21. // files were downloaded in reductive chain mode ...(after 9s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement