Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
How to grab value of promise and fire function

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();
      });
    
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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();
  });
about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!