Questions about the js promise queue

I encountered a problem about queues in the process of learning promise. I encountered a problem in development, which requires an API for cyclic request for upload, but the following method does not seem to achieve the queue, so I beg God to help me transform

.

requirements are executed one after another, until the last one, serial, and do not quite understand the concept of columns

.
function queue(files) {
  let promise = Promise.resolve();
  files.forEach(file => {
    promise = promise.then(() => {
      return new Promise(resolve => {
         doThing(file, () => {
           //
             resolve();
           });
      });
    });
  });
  return promise;
}

queue([file1, file2, file3]).then(data => {
  console.log(data);
});
Mar.12,2021

excuse me, what are you going to do? Do you follow the queue or something?

paste a jsfiddle, which is executed according to the queue

https://jsfiddle.net/weisiwu/.


the resolve of a promise can only be used once, and the second time is invalid.
you need RxJs


The problem with

is that this is not a queue, but that foreach executes promise and recurses it.

function doThing(file) {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('doThing', file)
      resolve('result:' + file)
    }, 500)
  })
}

function queue(files, data = []) {
  return new Promise((resolve) => {
    if (files.length > 0) {
      let file = files.shift();
      doThing(file).then((res) => {
        data.push(res)
        resolve(queue(files, data))
      })
    } else {
      resolve(data)
    }
  })
}

queue(['file1', 'file2', 'file3']).then(data => {
  console.log(data);
});

I don't know, do you want to achieve serial upload or parallel upload?

/ / below is
function queue (arr, handle) {
let index = 0
let length = arr.length

return new Promise ((resolve, reject) = > {

!(function next() {
  try {
    handle(arr[index])
      .then(function () {
        PPindex < length
          ? next()
          : resolve()
      })
      .catch(reject)
  } catch (err) {
    reject(err)
  }
})()

})
}

function upload (file) {
return new Promise ((resolve, reject) = > {

// 
setTimeout(function () {
  console.log(`upload ${file}`)
  resolve()
}, file * 1000 )

})
}

queue (['3,'2,'1'], upload). Then (data = > {
console.log ('ok');
}) .catch (err = > {
console.log (' error', err)
})

)

Let me make some comments on your code and see for yourself what the problem is

.
// 
function queue(files) {

  // Promise.resolve(val)Promisepromise
  // promiseResolved
  // promisethenResolvedval(valundefined)
  let promise = Promise.resolve(); 
  
  // 
  files.forEach(file => {

    // promisethen,thenpromiseresolve
    // 
    // resolve()
    // rejectundefined
    promise = promise.then(() => { 

    // then.resolvePromise
    // "resolve"promiseresolve,reject
      return new Promise(resolve => { 

         // Promiseresolve
         doThing(file, () => { 
             
             //

             // resolvepromise
             // data
             resolve();
           });
      });
    });
  });
  // promisepromise
  return promise;
}

// queue
// 
// , resolvethen
queue([file1, file2, file3]) 

//promiseresolve
.then(data => { 

  // resolve
  console.log(data); 
});

in addition, I also have a note from learning promise here. You can review
my Promise notes

.

there is no problem with your queue, but you didn't return data in the end. If you need data, you can return it in resolve () .

in addition, I would like to introduce myself to my lecture hall: n uses of Promise , which not only introduces the various uses of Promise in detail, but also writes about the handling of two kinds of queues.

Menu