From fc7ffe5c9e0cb7cc60e90b1f2bb51afef45c77c3 Mon Sep 17 00:00:00 2001 From: Rui Fortes Date: Wed, 13 Dec 2017 19:16:37 +0000 Subject: [PATCH 1/2] Use index instead of shit to avoid changing input --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 0e1be3f..7d33f18 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,13 @@ module.exports = function flatmap (fn) { - var queue = [] + var queue = [], i return function (read) { return function again (abort, cb) { if(abort) return read(abort, cb) - if(queue.length) return cb(null, queue.shift()) - + if(i) return cb(null, queue[--i]) read(null, function (err, data) { if(err) return cb(err) queue = fn(data) + i = queue.length again(null, cb) //cb or read again if queue is empty. }) } From f32e7a6094583f2ed77b5eeed7e9f70cfe8c36b3 Mon Sep 17 00:00:00 2001 From: Rui Fortes Date: Wed, 13 Dec 2017 23:54:35 +0000 Subject: [PATCH 2/2] fix iteration order --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 7d33f18..f855cd6 100644 --- a/index.js +++ b/index.js @@ -2,12 +2,12 @@ module.exports = function flatmap (fn) { var queue = [], i return function (read) { return function again (abort, cb) { - if(abort) return read(abort, cb) - if(i) return cb(null, queue[--i]) + if(abort) return read(abort, cb) + if(i < queue.length) return cb(null, queue[i++]) read(null, function (err, data) { if(err) return cb(err) queue = fn(data) - i = queue.length + i = 0 again(null, cb) //cb or read again if queue is empty. }) }