The emitter emits several data events, one for each piece that it receives. However, a represents only one future meaning, in your case you want this to be the complete answer.
resolve must be called only once to fulfill the promise with the passed value, which is then set. Further calls will have no effect - and therefore you get only the first parts of your list.
Instead, you will need to accumulate all the data, and when the stream ends, you can fulfill the promise with all of this.
var Promise = require('bluebird'), download = require('download'), Buffer = require('buffer'); // should be global anyway exports = { downloadAsync: function promisifiedDownload() { var args = arguments, self = this; return new Promise(function(resolve, reject) { // We call the originalMethod here because if it throws, // it will reject the returned promise with the thrown error var emitter = download.apply(self, args); var buffers = []; emitter.on("data", function(data) { buffers.push(data); }).on("error", function(err) { reject(err); }).on("close", function() { resolve(Buffer.concat(buffers)); }); }); }; };
Note that using promisifyAll is pointless enough when you only want to promise one method. I skipped this for simplicity
You can also listen to the incoming response object and attach a data listener to it. Then you can use the end event instead of close .
Bergi source share