SHARE
TWEET

queue promises

a guest May 19th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const queuePromise = async ([promise, ...promises]) => {
  2.   console.log(promises.length + 1)
  3.   if (promises.length === 0) {
  4.     return
  5.   }
  6.  
  7.   await promise()
  8.   return queuePromise(promises)
  9. }
  10.  
  11. const makePromise = () => new Promise((resolve) => {
  12.   setTimeout(resolve, 1000)
  13. })
  14.  
  15. const makePromises = (count) => {
  16.   const makeIt = (count, promises) => {
  17.     if(count === 0) { return promises }
  18.     return makeIt(count - 1, [...promises, makePromise])
  19.   }
  20.   return makeIt(count, [])
  21. }
  22.  
  23. const promiseList = makePromises(10)
  24. queuePromise(promiseList).then(() => console.log('done'))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top