0

I'm developing a microservice in Loopback4/NodeJS and I need to call two REST services in parallel but return only when both returns. In this case, I need to create a new object from results and then return.

This is the signature of REST service function:

getUserById(id: string, attributes: string | undefined, excludedAttributes: string | undefined): Promise<UserResponseObject>;

And this is the way I'm trying to do (a sample code calling same service twice):

  async getUserById(@param.path.string('userId') userId: string): Promise<any> {

    console.log('1st call')
    const r1 = this.userService.getUserById(userId, undefined, undefined);

    console.log('2nd call...')
    const r2 = this.userService.getUserById(userId, undefined, undefined);

    await Promise.all([r1, r2]).then(function (results) {
      return results[0];
    });
  }

But it doesn't return anything (204).

I saw some examples around but it doesn't work in my case. What am I missing?

Thanks in advance,

Emilio Numazaki
  • 836
  • 1
  • 5
  • 25
  • The code actually looks fine (except from being clearly a prototype). What's result of `await getUserById('someUserId')`? What happens when you add logging into that `then`? – raina77ow Oct 11 '20 at 18:19
  • Breakpoint hits the line next to 'then' but function returned before. @DazDylz answer is the correct. – Emilio Numazaki Oct 11 '20 at 18:35

2 Answers2

1
async getUserById(@param.path.string('userId') userId: string): Promise<any> {

    console.log('1st call')
    const r1 = this.userService.getUserById(userId, undefined, undefined);

    console.log('2nd call...')
    const r2 = this.userService.getUserById(userId, undefined, undefined);

    return await Promise.all([r1, r2]).then(function (results) {
      return results[0];
    });
  }
DazDylz
  • 1,028
  • 3
  • 13
  • 39
0

It is doing something, it is infact returning even the status code 204, which stands for "no content" (list of all status codes here). So the status code pretty much implies that there is no content to be returned.

lanxion
  • 1,350
  • 1
  • 7
  • 20