Home > other >  How to grab value of promise and fire function
How to grab value of promise and fire function

Time:12-07

I know this is a common question asked, but I'm having a bit of an issue, trying to fire a function, ONLY when a promise has been returned. I also want to grab the value returned of the promise.

  • So I want to access an angular service, which seems to make an API call.
  • Wait for the promise to resolve.
  • Then store the promise returned as a value.
  • And then fire the function hello()

It seems when I fire run my code below, validPassengerId and hello()console.logs, before the promise has resolved.

enter image description here

How do I rewrite this code, to:

  • Wait for promise to resolve.

  • Then grab the value of the promise resolved - validPassengerId. (And then console.log it).

  • And then fire the function hello()?

      function hello() {
          console.log("hello");
      };
    
      const promise = new Promise((resolve, reject) => {
          let validPassengerId = _funnelBasketLuggageService.FindValidPassengerIdForBag(PassengerIds, bagsAssignedToPassengers, bag);
         resolve(validPassengerId);
      });
    
      promise.then(validPassengerId => {
          console.log("validPassengerId", validPassengerId);
          hello();
      });
    

CodePudding user response:

Assuming FindValidPassengerIdForBag returns a promise, you do not need to wrap it in another promise. You would use it like this:

_funnelBasketLuggageService
  .FindValidPassengerIdForBag(PassengerIds, bagsAssignedToPassengers, bag)
  .then(validPassengerId => {
    console.log("validPassengerId", validPassengerId);
    hello();
  });

If validPassengerId is null with this code, then that must be what FindValidPassengerIdForBag is resolving its promise to. If you weren't expecting that, check the code of FindValidPassengerIdForBag or check that you're passing in valid parameters.

CodePudding user response:

If in fact _funnelBasketLuggageService.FindValidPassengerIdForBag returns a promise, you solve this without creating your own Promise:

 function hello() {
      console.log("hello");
  };

  const promise = _funnelBasketLuggageService.FindValidPassengerIdForBag(PassengerIds, bagsAssignedToPassengers, bag);

  promise.then(validPassengerId => {
      console.log("validPassengerId", validPassengerId);
      hello();
  });

Explanation What you did was actually wrapping the Promise returned by FindValidPassengerIdForBag in another Promise that returns immediately.

If you absolutely have to wrap it in your own Promise, e.g. because you need to do some preprocessing, use .then instead of the return value of the function:

  function hello() {
      console.log("hello");
  };

  const promise = new Promise((resolve, reject) => {_funnelBasketLuggageService.FindValidPassengerIdForBag(PassengerIds, bagsAssignedToPassengers, bag).then(id=>resolve(id));
  });

  promise.then(validPassengerId => {
      console.log("validPassengerId", validPassengerId);
      hello();
  });
  • Related