1

I'm trying to get value from inside to outside then using promise.

In my controller I have:

async Sample(userId: number)
  {
      let data = this.userService.getAffectedExpertisesPromise(userId);
      await data.then((uri) =>
      {
          uri.forEach((exp: Expertise) =>
          {
              this.empList.push(exp.name);
          });
      })

      return this.empList;
  }

On ngOnInit, I call this function:

this.Sample(25).then(item =>
                    {
                        item.forEach((expLibelle: String) =>
                        {
                            listExp.push(expLibelle);
                        });
                        console.log("------------------------ List Size Inside: " + listExp.length);
                    });

      console.log("------------------------ List Size Outside : " + listExp.length);

In service user file, I have:

getAffectedExpertisesPromise(id: number): Promise<any>
    {
        return this.http.get(`${this.baseUrl}/users/expertisesObj/${id}`).toPromise();
    }

It produces:

------------------------ List Size Outside : 0

------------------------ List Size Inside: 3

As you saw:

  • the size inside then is 3 --> Correct answer
  • the size inside then is 0 --> Wrong answer

Could you please help me solving that issue ?.

Big thanks.

Jolia
  • 109
  • 1
  • 8
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – jonrsharpe Jul 05 '19 at 12:32

1 Answers1

3

Why not trying using XMLHttpRequest.

var request = new XMLHttpRequest();
request.open('GET', 'http://localhost:6227/api/auth/users', false);  
request.send(null);

HTH.

Saria Essid
  • 1,240
  • 1
  • 11
  • 16