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

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

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