Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
Devolver valor de la secuencia a la función principal

Tengo una secuencia para escribir texto en un depósito de almacenamiento en la nube. Al finalizar, quiero que se devuelva un mensaje a la función principal.

Intenté devolver el flujo de búfer, pero esto me da el objeto de flujo de búfer completo. Solo quiero que me devuelvan el mensaje.

Por ejemplo, si tengo otra función que llama a Bucket, quiero que me devuelva el mensaje de que el archivo se ha cargado para poder mostrarlo en el navegador.

¿Cómo puedo arreglar esto?

 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; }); };

para uso en

 () => async { await things happening... const saved = toBucket(message,filename); sendToBrowser(saved); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La función toBucket debería devolver una promesa, luego puede await en su función principal. Para hacer eso, simplemente envuelva la lógica de toBucket en una promesa

 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 }); }) };

En la función padre:

 () => async { await things happening... const saved = await toBucket(message,filename); // await sendToBrowser(saved); }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!