Skip to content

Commit a1bc431

Browse files
author
Einar Norðfjörð
committed
remove promise swallowing
1 parent 808aae2 commit a1bc431

File tree

2 files changed

+79
-26
lines changed

2 files changed

+79
-26
lines changed

lib/index.js

-7
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ flyd.fromPromise = function fromPromise(p) {
438438
return s;
439439
}
440440

441-
/* istanbul ignore next */
442441
flyd.flattenPromise = function flattenPromise(s) {
443442
return combine(function(s, self) {
444443
s().then(self);
@@ -671,12 +670,6 @@ function flushUpdate() {
671670
* @param {*} value
672671
*/
673672
function updateStreamValue(s, n) {
674-
/* istanbul ignore if */
675-
if (n !== undefined && n !== null && isFunction(n.then)) {
676-
console.warn('flyd: Promise swallowing has been deprecated, please see https://github.com/paldepind/flyd#promises for more info');
677-
n.then(s);
678-
return;
679-
}
680673
s.val = n;
681674
s.hasVal = true;
682675
if (inStream === undefined) {

test/index.js

+79-19
Original file line numberDiff line numberDiff line change
@@ -369,26 +369,86 @@ describe('stream', function() {
369369
});
370370
});
371371

372-
describe('promise swallowing', function() {
373-
it('pushes result of promise down the stream', function(done) {
374-
var s = flyd.fromPromise(Promise.resolve(12));
375-
combine(function(s) {
376-
assert.equal(s(), 12);
377-
done();
378-
}, [s]);
372+
describe('Promises', function() {
373+
describe('fromPromise', function() {
374+
it('pushes result of promise down the stream', function(done) {
375+
var s = flyd.fromPromise(Promise.resolve(12));
376+
combine(function(s) {
377+
assert.equal(s(), 12);
378+
done();
379+
}, [s]);
380+
});
381+
it('recursively unpacks promise', function(done) {
382+
var s = flyd.fromPromise(new Promise(function(res) {
383+
setTimeout(function() {
384+
res(new Promise(function(res) {
385+
setTimeout(res.bind(null, 12));
386+
}));
387+
}, 20);
388+
}));
389+
combine(function(s) {
390+
assert.equal(s(), 12);
391+
done();
392+
}, [s]);
393+
});
394+
395+
it('does not process out of order promises', function(done) {
396+
var promises = [];
397+
var delay = function(ms, val) {
398+
var p = new Promise(function(res) {
399+
setTimeout(function() {
400+
res(val);
401+
}, ms)
402+
});
403+
promises.push(p);
404+
return p;
405+
};
406+
407+
var s = stream();
408+
var res = s.chain(function(val) {
409+
return flyd.fromPromise(delay(val, val));
410+
})
411+
.pipe(flyd.scan(function(acc, v) {
412+
return acc + v;
413+
}, 0));
414+
s(100)(50)(70)(200);
415+
416+
Promise.all(promises).then(function() {
417+
assert.equal(res(), 200);
418+
done();
419+
});
420+
421+
});
379422
});
380-
it('recursively unpacks promise', function(done) {
381-
var s = flyd.fromPromise(new Promise(function(res) {
382-
setTimeout(function() {
383-
res(new Promise(function(res) {
384-
setTimeout(res.bind(null, 12));
385-
}));
386-
}, 20);
387-
}));
388-
combine(function(s) {
389-
assert.equal(s(), 12);
390-
done();
391-
}, [s]);
423+
describe('flattenPromise', function() {
424+
it('processes out of order promises', function(done) {
425+
var promises = [];
426+
var delay = function(ms, val) {
427+
var p = new Promise(function(res) {
428+
setTimeout(function() {
429+
res(val);
430+
}, ms)
431+
});
432+
promises.push(p);
433+
return p;
434+
};
435+
436+
var s = stream();
437+
var res = s.map(function(val) {
438+
return delay(val, val);
439+
})
440+
.pipe(flyd.flattenPromise)
441+
.pipe(flyd.scan(function(acc, v) {
442+
return acc + v;
443+
}, 0));
444+
s(100)(50)(70)(200);
445+
446+
Promise.all(promises).then(function() {
447+
assert.equal(res(), 420);
448+
done();
449+
});
450+
451+
});
392452
});
393453
});
394454

0 commit comments

Comments
 (0)