How to queue for Promises in Javascript?

Asked

Viewed 227 times

0

I would like to know the best way to make a row of Promises in Javascript, so that the incoming Promise only runs after the previous Promise resolution.

Example:

var queue = [Promise1, Promise2];
queue.push(Promise3);

In that case I need Promise2 await the execution of Promise1 and the Promise3 await the execution of Promise2 to be executed.

In reality I need to queue to save data in a request and if I do not save in the order that the request was made the data get messy because one result depends on the answer of the other.

If you can help me I’d be grateful!

I’ll try to explain it better.

I launch a request to create a record with a note...but this record consists of 4 notes in total...the next note for this record is an update...what happens is that the release of notes can be so fast that when sending the request to create the record not from time to receive the created id so that the next one is an update, this way I end up with two records created which should be a create and then an update.

  • If a Promise depends on the result of the previous one, how can you instantiate it without the result you need? Because it depends on the outcome of the previous one, in my view you could only instantiate it at the moment Promise earlier had already resolved.

  • And you can’t send the 4 notes in the same request?

  • Unfortunately I only have the front to work, I do not have access to change the back functioning.

3 answers

2

If the callback of the method Promise.then() return a Promise is made the chaining, so just return to Promise of the new request and continue chaining then().

Simple example:

// Retorna um Promise
function wait(seconds) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(seconds + " segundos se passaram...")
        }, seconds * 1000)
    })
}

wait(3)
  .then(msg => {
    console.log(msg)  // resultado de wait(3)
    return wait(1)
  }).then(msg => {
    console.log(msg)  // resultado de wait(1)
    return wait(2)
  }).then(msg => {
    console.log(msg)  // resultado de wait(2)
    return wait(3)
  }).then(msg => {
    console.log(msg)  // resultado de wait(3)
  }).finally(() => {
    console.log("fim...")
  })

0


Hello thanks so much for the help...

With the help of this repository, (angular-Queue) i made an implementation of a queue that does what I was needing if you want to take a look at the example of the code I did in Plunker (Promise-Queue).

Anyway thank you very much and I hope I can help someone else with this.

0

Good afternoon Rafael,

If I understand your question correctly, I recommend using the library at link. Below is an example, which is possibly your answer:

return Q.all([
    eventualAdd(2, 2),
    eventualAdd(10, 20)
]);

Avoid as much as possible, unless it is an academic work, reinvent the wheel. I hope to have contributed to the solution of your question.

Have a great afternoon.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.