Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.


untitled notebook

node v0.10.48
version: 1.0.0
var Promise = require("bluebird").Promise; var Readable = require('stream').PassThrough; var rs = new Readable; rs.push("beep"); rs.push("boo"); var data = await readAtMost(rs, 10); console.log(data); rs.push("boo"); var data = await readAtMost(rs, 100); console.log(data);
function waitForEvent(anEventEmitter, anEvent) { return new Promise(function(resolve, reject) { anEventEmitter.once(anEvent, function() { resolve(Array.prototype.slice.apply(arguments)); }); }); }
function readAtMost(stream, amount) { return new Promise(async function(resolve, reject) { // Must be less than amount. var data = stream.read(amount) || stream.read(); if (data !== null) return resolve(data); await waitForEvent("readable"); return readAtMost(stream, amount); }); }

no comments

    sign in to comment