Skip to content

Instantly share code, notes, and snippets.

@bengl bengl/funfunfunfun.js
Last active Sep 13, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.