diff --git a/lib/index.js b/lib/index.js index e06d8c2..958a5db 100644 --- a/lib/index.js +++ b/lib/index.js @@ -438,7 +438,6 @@ flyd.fromPromise = function fromPromise(p) { return s; } -/* istanbul ignore next */ flyd.flattenPromise = function flattenPromise(s) { return combine(function(s, self) { s().then(self); @@ -671,12 +670,6 @@ function flushUpdate() { * @param {*} value */ function updateStreamValue(s, n) { - /* istanbul ignore if */ - if (n !== undefined && n !== null && isFunction(n.then)) { - console.warn('flyd: Promise swallowing has been deprecated, please see https://github.com/paldepind/flyd#promises for more info'); - n.then(s); - return; - } s.val = n; s.hasVal = true; if (inStream === undefined) { diff --git a/test/index.js b/test/index.js index beee4bc..d1c8891 100644 --- a/test/index.js +++ b/test/index.js @@ -369,26 +369,86 @@ describe('stream', function() { }); }); - describe('promise swallowing', function() { - it('pushes result of promise down the stream', function(done) { - var s = flyd.fromPromise(Promise.resolve(12)); - combine(function(s) { - assert.equal(s(), 12); - done(); - }, [s]); + describe('Promises', function() { + describe('fromPromise', function() { + it('pushes result of promise down the stream', function(done) { + var s = flyd.fromPromise(Promise.resolve(12)); + combine(function(s) { + assert.equal(s(), 12); + done(); + }, [s]); + }); + it('recursively unpacks promise', function(done) { + var s = flyd.fromPromise(new Promise(function(res) { + setTimeout(function() { + res(new Promise(function(res) { + setTimeout(res.bind(null, 12)); + })); + }, 20); + })); + combine(function(s) { + assert.equal(s(), 12); + done(); + }, [s]); + }); + + it('does not process out of order promises', function(done) { + var promises = []; + var delay = function(ms, val) { + var p = new Promise(function(res) { + setTimeout(function() { + res(val); + }, ms) + }); + promises.push(p); + return p; + }; + + var s = stream(); + var res = s.chain(function(val) { + return flyd.fromPromise(delay(val, val)); + }) + .pipe(flyd.scan(function(acc, v) { + return acc + v; + }, 0)); + s(100)(50)(70)(200); + + Promise.all(promises).then(function() { + assert.equal(res(), 200); + done(); + }); + + }); }); - it('recursively unpacks promise', function(done) { - var s = flyd.fromPromise(new Promise(function(res) { - setTimeout(function() { - res(new Promise(function(res) { - setTimeout(res.bind(null, 12)); - })); - }, 20); - })); - combine(function(s) { - assert.equal(s(), 12); - done(); - }, [s]); + describe('flattenPromise', function() { + it('processes out of order promises', function(done) { + var promises = []; + var delay = function(ms, val) { + var p = new Promise(function(res) { + setTimeout(function() { + res(val); + }, ms) + }); + promises.push(p); + return p; + }; + + var s = stream(); + var res = s.map(function(val) { + return delay(val, val); + }) + .pipe(flyd.flattenPromise) + .pipe(flyd.scan(function(acc, v) { + return acc + v; + }, 0)); + s(100)(50)(70)(200); + + Promise.all(promises).then(function() { + assert.equal(res(), 420); + done(); + }); + + }); }); });