OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to make Firebase Cloud Function wait for a stream to close before terminating

  • Thread starter Thread starter starleaf1
  • Start date Start date
S

starleaf1

Guest
I store an object to Cloud Storage like this:

Code:
const bucket = getStorage().bucket();
const fileRef = bucket.file("/foo/bar");
const storageWriteStream = fileRef.createWriteStream();

const myFileStream = someLibraryThatCreatesAStream();

storageWriteStream.on("error", (err) => {
  functions.logger.error(err);
 });

storageWriteStream.on("close", (err) => {
  return await getDownloadURL(fileRef)
});

myFileStream.pipe(storageWriteStream);

But when called on the front-end, the result is always null. Since Functions seems to always has trouble with return as part of an event handler, I think it's because Functions doesn't wait for storageWriteStream's close event.

How do I get around this?

<p>I store an object to Cloud Storage like this:</p>
<pre class="lang-js prettyprint-override"><code>const bucket = getStorage().bucket();
const fileRef = bucket.file("/foo/bar");
const storageWriteStream = fileRef.createWriteStream();

const myFileStream = someLibraryThatCreatesAStream();

storageWriteStream.on("error", (err) => {
functions.logger.error(err);
});

storageWriteStream.on("close", (err) => {
return await getDownloadURL(fileRef)
});

myFileStream.pipe(storageWriteStream);
</code></pre>
<p>But when called on the front-end, the result is always <code>null</code>. Since Functions seems to always has trouble with <code>return</code> as part of an event handler, I think it's because Functions doesn't wait for <code>storageWriteStream</code>'s <code>close</code> event.</p>
<p>How do I get around this?</p>
 
Top