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

98
Visualizações
Returning value from stream to parent function

I have a stream for writing some text to a cloud storage bucket. On finish I want a message to be returned to the parent function.

I tried returning the bufferstream but this gives me the whole bufferstream object. I just want the message to be returned.

e.g. if I have another function calling toBucket I want the message that the file has been uploaded being returned so I can show it in the browser.

How can I fix this?

const toBucket = (message, filename) => {
  const storage = new Storage();
  // Initiate the source
  const bufferStream = new stream.PassThrough();
  // Write your buffer
  bufferStream.end(Buffer.from(message));

  const myBucket = storage.bucket(process.env.BUCKET);
  const file = myBucket.file(filename);
  // Pipe the 'bufferStream' into a 'file.createWriteStream' method.
  bufferStream
    .pipe(
      file.createWriteStream({
        validation: 'md5',
      })
    )
    .on('error', (err) => {
      // eslint-disable-next-line no-console
      console.error(err);
    })
    .on('finish', () => {
      // The file upload is complete.
      const message = `${filename} is uploaded!`;
      // eslint-disable-next-line no-console
      console.log(message);
      return message;
    });
};

for use in

() => async {
await things happening...

const saved = toBucket(message,filename);

sendToBrowser(saved);

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

0

The toBucket function should return a promise, then you can await it in your parent function. To do that, just wrap the logic of toBucket into a promise

const toBucket = (message, filename) => {
    return new Promise((resolve, reject) => { // return a promise
        const storage = new Storage();
        // Initiate the source
        const bufferStream = new stream.PassThrough();
        // Write your buffer
        bufferStream.end(Buffer.from(message));

        const myBucket = storage.bucket(process.env.BUCKET);
        const file = myBucket.file(filename);
        // Pipe the 'bufferStream' into a 'file.createWriteStream' method.
        bufferStream
            .pipe(
                file.createWriteStream({
                    validation: 'md5',
                })
            )
            .on('error', (err) => {
                // eslint-disable-next-line no-console
                console.error(err);
                reject(err); // reject when something went wrong
            })
            .on('finish', () => {
                // The file upload is complete.
                const message = `${filename} is uploaded!`;
                // eslint-disable-next-line no-console
                console.log(message);
                // return message;
                resolve(message); // return message and finish
            });
    })
};

In the parent function:

() => async {
    await things happening...
    
    const saved = await toBucket(message,filename); // await
    
    sendToBrowser(saved);
}
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