1

Hi I want to return the response after a file is uploaded to firebase right now I can upload but I want the file to be uploaded before returning a response

const file = bucket.file(`equipo_${idEquipo}`);
  const fileStream = file.createWriteStream({ resumable: false });
  fileStream.end(newImg.buffer);
  fileStream.on("error", err => console.log(err));
  fileStream.on("finish", () => {
  }); 
return response;
  • A writeStream is asynchronous so your function has to either utilize a callback to return the result or needs to return a Promise. – t.niese Apr 29 '21 at 16:57
  • How can I return a promise of this code? and wait to be resolved – miki castro Apr 29 '21 at 18:11
  • The linked duplicate shows how you can wrap your code into a promise and call `resolve` on `finish` and `reject` on `error` – t.niese Apr 29 '21 at 19:45

1 Answers1

0

You have the filestram callback already so

const file = bucket.file(`equipo_${idEquipo}`);
 const fileStream = file.createWriteStream({ resumable: false });
fileStream.end(newImg.buffer);
fileStream.on("error", err => console.log(err));
fileStream.on("finish", (response) => {
return response;
}); 
proxim0
  • 1,418
  • 2
  • 11
  • 14
  • And how should the `return` statement in the callback propagate to the outer function? – t.niese Apr 29 '21 at 16:51
  • You need to post the full code block to know for sure, thats not part of the question. you are 'returning' the response so that would indicate this is part of a function returning in response to a function so let upload = uploadFunction() would return response etc – proxim0 Apr 29 '21 at 16:55
  • I'm not the one who wrote the question. But the `return` in the finish callback won’t return anything from the `uploadFunction` independent from how that function actually looks like. – t.niese Apr 29 '21 at 16:59