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

105
Visualizações
Promise can't complete the calculation

I have a pretty easy formula which does not work inside Promise. Problem is following - whatever I put for parameters a and b, I always get failure, even though its clearly more thank 0. I am pretty sure that I am missing something but I can't understand what exactly, because with other formula it works (maybe I cannot call the function inside the Promise? in that case what would be the other way?). Sorry for noob question, Im new to it

const pidr = new Promise((resolve, reject) => {

    const result = (a, b) => (a + b) ** 2
    result(5, 6)

    if (result > 0) {
      resolve()
    } else {
      reject()
    }
  })

  .then(() => console.log('Success'))
  .catch(() => console.error('Failure'))
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need to create variable and set result of function execution. See below:

  const pidr = new Promise((resolve, reject) => {

    const result = (a, b) => (a + b) ** 2

    const res = result(5, 6)

    if (res > 0) {
      resolve()
    } else {
      reject()
    }
  })

  .then(() => console.log('Success'))
  .catch(() => console.error('Failure'))
about 4 years ago · Juan Pablo Isaza Relatório

0

This:

result(5, 6);

calls the function, but does not save the returned value. You then compare result (which is a function) to 0, and that doesn't make sense.

Instead:

const answer = result(5, 6);

if (answer > 0) {
  resolve()
} else {
  reject()
}
about 4 years ago · Juan Pablo Isaza Relatório

0

result is a function and it doesn't have to be, but if you want it to be a function you can call it inside of your if, like so:

const pidr = new Promise((resolve, reject) => {

    const result = (a, b) => (a + b) ** 2

    if (result(5, 6) > 0) {
      resolve()
    } else {
      reject()
    }
  })

  .then(() => console.log('Success'))
  .catch(() => console.error('Failure'))
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