Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda