1

Now in .then section I all another promise http request:

.then(result => { this.service().then(data => {}); });

Is it a correct way to use chained promises?

Daniel
  • 3,541
  • 3
  • 33
  • 46
POV
  • 11,293
  • 34
  • 107
  • 201
  • It depends on your task: if you use {{result}} from 1st request in 2nd (e.g. some parameters), then yes, that is right way. But if you need to make 2 calls in parallel, you can combine 2 promises – A. Tim Aug 28 '17 at 20:21
  • No. This is called Callback Hell, the exact thing that promises are supposed to fight with. – Estus Flask Aug 28 '17 at 20:26

3 Answers3

6

Almost! You need to return the promise in the function, either like this:

.then(result => { return this.service().then(data => {}); });

or like this:

.then(result => this.service().then(data => {}));
Ed.
  • 1,992
  • 1
  • 13
  • 30
  • Where is the point of returning that new promise when you are not using it? – Daniel Aug 28 '17 at 20:37
  • 2
    @Daniel Because Promise represents a value. One should think of it as a value (available some time later) in first place not some fancy callback system. – Yury Tarabanko Aug 28 '17 at 20:44
2

Since you are using Typescript, you could use async/await to chain promises in a more readable way:

function firstCall(): Promise<any> { /* return a promise */ }
function service(): Promise<any>{ /* return a promise */ }

async function runPromisses() {
    var result = await firstCall();
    var data = await service();
    // ...
}
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357
1

Yes, your two promises are resolved sequentially. But remember that your second (the inner) promise will only be called when the first then was successfully resolved.

An even cleaner solution would be:

.then(result => this.service()).then(data => {});

As elaborated in this SO answer by Hrishi, returning a "thenable" (for example a Promise) inside your then() function, makes the old promise adopt the state of the new promise.

Daniel
  • 3,541
  • 3
  • 33
  • 46
  • Not really. This is an anti-pattern called [the broken chain](http://taoofcode.net/promise-anti-patterns/) – Yury Tarabanko Aug 28 '17 at 20:26
  • @YuryTarabanko are you sure? My understanding was that the broken chain anti pattern happens when you add a `then()` to your promise, but return the original promise afterwards (which hasn't that appended `then()`). In my example, any errors could be caught with a single `catch()`. – Daniel Aug 28 '17 at 20:37
  • 1
    Quite sure. Swallowing inner promises inside `then` does the same problems to error handling as if you returned original promise (even worse). Your example is totally different code. It doesn't break promise chain (provided that you are going to return something from within `data => {}` – Yury Tarabanko Aug 28 '17 at 20:40
  • @YuryTarabanko Ah, I thought you were talking about my example. OPs code fits the broken chain pattern. I'm all with you on this one. – Daniel Aug 28 '17 at 20:46
  • 1
    :) When I left the comment there were no code here. So yes my comment was about OP's code . – Yury Tarabanko Aug 28 '17 at 20:50