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

201
Visualizações
How can I recover from node stream error?

Is it possible to recover from a node stream error? For example with promises you can do something like

async function getThumbnail(id, width, height) {
    try {
        const thumbnail = await readThumbnail(id, width, height);
        return thumbnail;
    } catch {
        // The thumbnail doesn't exist, so we will create and cache it
        const thumbnail = await createThumbnail(id, width, height);
        return thumbnail;
    }
}

If readThumbnail() returns a stream rather than a promise is it possible to recover from a stream error if the thumbnail doesn't exist? The goal is for the getThumbnail() method to always return a working stream regardless of whether the thumbnail exists or not.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I found a way to to do it using a third stream

function getThumbnail(id, width, height) {
    const readable = new PassThrough();

    readThumbnail(id, width, height)
        // It is important that this error handler is above the pipe
        .on('error', error => {
            createThumbnail(id, width, height).pipe(readable);
        })
        .pipe(readable);

    return readable;
}
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