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

276
Visualizações
UnhandledPromiseRejectionWarning: Reject an unhandled promise when processing certain cases with the question

I have this question that addresses a bunch of cases:

Your function must always return a promise
If data is not a number, return a promise rejected instantly and give the data "error" (in a string)
If data is an odd number, return a promise resolved 1 second later and give the data "odd" (in a string)
If data is an even number, return a promise rejected 2 seconds later and give the data "even" (in a string)

I typed the code for the question, but I got this error:

(node:6) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: data is not defined
(node:6) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

This is the code:

const job = new Promise((resolve, reject)=>{
    if(isNaN(data)){
        reject('erroe')
    }
    else if(isNaN(data) && data%2!=0){
        setTimeout(function(){
            resolve('odd')
        } , 1000)
    }
    else {
        setTimeout(function(){
            reject('even')
        }, 2000)
    }
})


module.exports = job;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here's what you can do in a concise way:

const job = data => new Promise((resolve, reject) =>
  typeof(data) === "number" 
  ? setTimeout(() => resolve(data % 2 ? 'odd' : 'even'), 1000) 
  : reject("error"))

job("s")
  .then(result => console.log(result))
  .catch(error => console.log(error));
job(5)
  .then(result => console.log(result))
  .catch(error => console.log(error))
job(2)
  .then(result => console.log(result))
  .catch(error => console.log(error))

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