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

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

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 Denunciar

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 Denunciar

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 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