tux-world tux-world - 3 months ago 15
Javascript Question

Nodejs create simple queue on Async

in this below sample code i can run multi function on the same time

Promise.all([sendMoneyToRequestedUser(_data), saveTransferMoneyTransaction(_data)])
.then(function (results) {
log.info("OOOOOOOOOOOOOo");
}).catch(function (error) {
log.info(error)
});


but i want to create simple queue for them, for example, can i implementing that with
Promise
with this solution? I have 3 functions
check
,
send
, and
post
, that I want to run in series and pass the result of each step to the other.

Step 1,
check()
function, Step 2,
send()
function and then after finish them execute function for example
post()


for this implementation i need to get result of each steps, for example get result from Step 1, if its return true then on Step 2 i need to use result of Step 1,

is
async.parallel
solution for this implementation? or
Promise
can doing that

Answer

UPDATE: The question is not really clear on what is given, and what is desired. From the comments, I've got the following:

three functions of check, send, and post are given and it is desired to be run in series.

Let's say that the mentioned check, send, and post all return Promises, e.g.:

function check() {
  return new Promise(function (resolve, reject) {
    // Do some stuff and save them in a var 'results'
    var results = ...;
    resolve(results);
  });
}

Then you can simply build a queue as follows:

check()
  .then(send)
  .then(post)
  .catch(function(err) { console.error(err); })

At each level the function is called with the value resolved from the previous level.