Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda