Skip to content

Commit

Permalink
remove promise swallowing
Browse files Browse the repository at this point in the history
  • Loading branch information
Einar Norðfjörð committed Feb 1, 2018
1 parent 808aae2 commit 3604505
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ __Other features__

* Supports the transducer protocol. You can for instance transduce streams with
[Ramda](http://ramdajs.com/) and [transducers.js](https://github.com/jlongster/transducers.js).
* Conforms to the fantasy land [monad](https://github.com/fantasyland/fantasy-land#monad) specification
* [Atomic updates](#atomic-updates).

## Examples
Expand Down
7 changes: 0 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
98 changes: 79 additions & 19 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

});
});
});

Expand Down

0 comments on commit 3604505

Please sign in to comment.