Allow asynchronous callback functions in filter
.
This commit is contained in:
parent
cd1a4a4e96
commit
cdfb4655b5
2 changed files with 7 additions and 9 deletions
14
lib/index.js
14
lib/index.js
|
@ -161,17 +161,15 @@ const Channel = function(length = 0) {
|
||||||
|
|
||||||
filter: (callbackfn, thisArg) => {
|
filter: (callbackfn, thisArg) => {
|
||||||
const output = Channel();
|
const output = Channel();
|
||||||
(async () => {
|
|
||||||
for (;;) {
|
|
||||||
const value = await readOnly.shift();
|
|
||||||
|
|
||||||
if (value === undefined) {
|
(async () => {
|
||||||
await output.close();
|
await readOnly.forEach(async value => {
|
||||||
break;
|
if (await callbackfn.call(thisArg, value)) {
|
||||||
} else if (callbackfn.call(thisArg, value)) {
|
|
||||||
await output.push(value);
|
await output.push(value);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
await output.close();
|
||||||
})();
|
})();
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
|
|
|
@ -244,7 +244,7 @@ describe(`Channel object`, function() {
|
||||||
it(`filter`, async function() {
|
it(`filter`, async function() {
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
await Channel.of(0, 1, 2, 3, 4, 5)
|
await Channel.of(0, 1, 2, 3, 4, 5)
|
||||||
.filter(value => value % 2 !== 0)
|
.filter(async value => value % 2 !== 0)
|
||||||
.values(),
|
.values(),
|
||||||
[1, 3, 5]
|
[1, 3, 5]
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue