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

234
Visualizações
Node throws unhandled promise rejection while using rejected promise as input parameter

I discovered a weird issue while trying to implement a function that awaits all promises of an array and catches all errors. This implementation led to a complete application crash caused by an unhandled promise rejection.

const completeAllPromises = async (promises) => {
    const errors = []

    for (let i = 0; i < promises.length; i++) {
        await promises[i].catch((error) => {
            error.message = `${i}: ${error.message}`
            errors.push(error)
        })
    }

    if (errors.length) return Promise.reject(errors)
}

const timeout = (duration) => {
    return new Promise((resolve) => setTimeout(() => resolve()), duration)
}

const fail = async () => {
    return Promise.reject(new Error('Failed'))
}

const crash = async () => {
    await completeAllPromises([timeout(100), fail()]).catch(
        (error) => console.log(`Crash prevented, catch promise rejection`, error), // never be called
    )
}

const main = async () => {
    // will crash and catch will not be called
    await crash().catch((error) => console.log('Finished crash() and catch error', error))
}

main()

It seems like the catch implementation in completeAllPromises function is executed at runtime, so while awaiting the first timeout promise, fail() will reject and not be catch. The weird thing for me is that the crash() and also not main() will catch this error. It almost seems like the call chain is not resolvable at the time fail() rejects.

Switching timeout(100) and fail() will result in a properly catch errors. Using a try and catch block will also lead to the same issue.

Solution ✅

const completeAllPromises = async (promises) => {
    const errors = []
    const results = await Promise.allSettled(promises)
    results.forEach((result) => {
        if (result.status === 'rejected') errors.push(result.reason)
    })
    if (errors.length) return Promise.reject(errors)
}

const timeout = (duration) => {
    return new Promise((resolve) => setTimeout(() => resolve()), duration)
}

const fail = async () => {
    return Promise.reject(new Error('Failed'))
}

const crash = async () => {
    await completeAllPromises([timeout(100), fail()]).catch(
        (error) => console.log(`Crash prevented, catch promise rejection`, error), // never be called
    )
}

const main = async () => {
    // will crash and catch will not be called
    await crash().catch((error) => console.log('Finished crash() and catch error', error))
}

main()
about 4 years ago · Juan Pablo Isaza
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