0

I have the following requirement,I have three asynchronous function async1(),async2(),ascync3() which all return promises Now I would call one function and serially execute async1,async2,async3 respectively and I want to print the resolved promise returned after async3

this is my main function

testPromiseSerially = function() {
  return new promise(function(resolve,reject) {
   async1().
   then(function(result1) {
    return async2(result1)
   })
   .then(function(result2){
     return async3(result2)
   })
   .catch(function(err) {
     return reject(err) 
   }
 })
}

This is my async3 function

async3 = function(params) {
 return new promise(function(resolve,reject) {
   return resolve("solved")
})
}

and async1 and async2 are also similar to async3

If I execute this code

testPromiseSerially.then(function(result) {
  console.log(result)
})
.catch(function (err) {
 console.log(err) 
})

testPromiseSerially is getting called but it's not entering 'then' or 'catch' block.Is promise returned by async3 not relayed back to testpromiseSerially()? How do I see the result from async3? I know that if I extend my code like adding

.then(function(result) {
  return resolve(result)
})

after async3(result) then I would be able to see the result. But I have chain of functions which depend on promise returned by other functions, so how do I handle that?

Shakthi
  • 131
  • 2
  • 15
  • Avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572)! – Bergi Apr 21 '16 at 12:31

2 Answers2

3

The main problem is that your testPromiseSerially code is never calling resolve. So the promise it returns is never resolved.

Since what it's testing already has promises, there's no need for you to create a new one. Every call to then produces a new promise, so just use that.

Additionally, this:

.then(function(result1) {
    return async2(result1);
})

is more complicated/verbose than you need, it can be just:

.then(async2)

And the same for the .catch.

So:

let testPromiseSerially = function() {
  return async1()
      .then(async2)
      .then(async3);
};

Example using JavaScript's native promises on Babel's REPL

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks for the reply, but in this case async2() depends on the result of async1() so I was using the code .then(function(result1) { return async2(result1); }) . if I use the same code as you mentioned above will async2() be called with result from async1()? – Shakthi Apr 22 '16 at 04:15
  • 1
    @Shakthi: Yes, for the same reason `.then(function(result1) { })` is called with the result of `async1`. In both cases, you're passing a function reference into `then`, and in both cases, the promise will call the function you passed with the promise's settled value. I've updated the code in the Babel REPL example to demonstrate that. – T.J. Crowder Apr 22 '16 at 06:39
0

You should use async https://github.com/caolan/async. It is be better for your case. Look at the waterfall function.

From the documentation

waterfall(tasks, [callback])

Runs the tasks array of functions in series, each passing their results to the next in the array. However, if any of the tasks pass an error to their own callback, the next function is not executed, and the main callback is immediately called with the error.
martin karl
  • 48
  • 1
  • 7