eugeneware / gifencoder

Server side animated gif generation for node.js
Other
472 stars 49 forks source link

Wait for fs.createWriteStream to be done? #35

Closed anishkny closed 6 years ago

anishkny commented 6 years ago

Hi folks,

Thanks for this amazing repo! Wondering how I could wait for the PNG file creation to be done in this example from the README

var GIFEncoder = require('gifencoder');
var encoder = new GIFEncoder(854, 480);
var pngFileStream = require('png-file-stream');
var fs = require('fs');

pngFileStream('test/**/frame?.png')
  .pipe(encoder.createWriteStream({ repeat: -1, delay: 500, quality: 10 }))
  .pipe(fs.createWriteStream('myanimated.gif'));

We would like to post-process the generated PNG but need to wait for it to be done (https://github.com/xiegeo/commit-sudoku/issues/92).

heikkipora commented 6 years ago

Wait for the 'finish' event from the stream.

var stream = pngFileStream('test/**/frame?.png')
  .pipe(encoder.createWriteStream({ repeat: -1, delay: 500, quality: 10 }))
  .pipe(fs.createWriteStream('myanimated.gif'));

stream.on('finish', function () {
  // do stuff
})

You cannot await the stream directly, but you can wrap the finish event to a promise which you can await for (from the top of my head..)

await new Promise((resolve, reject) => {
  stream.on('finish', resolve)
  stream.on('error', reject)
})