6

I have a piece of code that looks like this:

func().then(function (result){
   var a = func1(result);
   func2(a).then(function(result1){
    ////
   }
}

As you can see func returns a promise, and in then part we call another func1 which also returns a promise. Is it possible to chain the promise returned from func2 with the promise of then, and somehow get ride of the nested functions in the second then.

Arash
  • 11,697
  • 14
  • 54
  • 81

1 Answers1

11

The return value inside a then() function is used as a promise value itself. So you can easily return the new promise and keep on chaining here:

func()
  .then(function (result){
    var a = func1(result);
    return func2(a);
  })
  .then(function(result1){
    ////
  })

See 2.2.7 and 2.3.2 of the Promise A+ Spec.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Sirko
  • 72,589
  • 19
  • 149
  • 183