diff --git a/index.js b/index.js index 6cc782e..d80c8ca 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,10 @@ function StreamArray(list) { StreamArray.prototype = Object.create(Readable.prototype, {constructor: {value: StreamArray}}); StreamArray.prototype._read = function(size) { - this.push(this._i < this._l ? this._list[this._i++] : null); + var self = this; + process.nextTick(function() { + self.push(self._i < self._l ? self._list[self._i++] : null); + }); }; module.exports = function(list) { diff --git a/test/5.js b/test/5.js new file mode 100644 index 0000000..2947436 --- /dev/null +++ b/test/5.js @@ -0,0 +1,27 @@ +var test = require('tape') + , streamify = require('..') + , concat = require('concat-stream') +; + +test('async, non-blocking', function(t) { + var s = streamify(['1', '2', '3']); + + var i = 0; + s.on('data', function(item) { + i++; + }); + + process.nextTick(function() { + t.equal(0, i, 'should emit the first item after this code'); + process.nextTick(function() { + t.equal(1, i, 'should emit the second item after this code'); + process.nextTick(function() { + t.equal(2, i, 'should emit the third item after this code'); + process.nextTick(function() { + t.equal(3, i, 'should have emitted all three items in the fourth tick'); + t.end(); + }); + }); + }); + }); +});