Skip to content

Instantly share code, notes, and snippets.

@bengl
Last active September 13, 2017 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bengl/24d58254d2d60144485d8763f20e566c to your computer and use it in GitHub Desktop.
Save bengl/24d58254d2d60144485d8763f20e566c to your computer and use it in GitHub Desktop.
const { Readable } = require('stream');
const oldOn = Readable.prototype.on;
Readable.prototype.on = function on(name, fn) {
oldOn.apply(this, arguments);
if (name !== 'allData' || this._readableState.objectMode) {
return;
}
const bufs = [];
oldOn.call(this, 'data', d => bufs.push(d));
oldOn.call(this, 'end', () => {
this.emit('allData',
Buffer.isBuffer(bufs[0]) ? Buffer.concat(bufs) : bufs.join(''));
});
return this;
};
Readable.prototype.addListener = Readable.prototype.on;
require('events').prototype.whence = function (name) {
return new Promise(res => this.once(name, res));
};
// --------------------------
const https = require('https');
async function get (url) {
const res = await https.get(url).whence('response');
return res.whence('allData');
}
(async function main () {
console.log(await get('https://nodejs.org'));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment