Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
array-async-filter
Advanced tools
Filter an array using async filter callback.
example/filter.js:
var asyncFilter = require('..');
asyncFilter(
['abc', 'bcd', 'cde', 'def'],
function (val, i, arr, next) {
process.nextTick(function () {
next(null, val.indexOf('cd') !== -1);
});
},
function (err, res) {
console.log('async callback:', err, res);
}
);
asyncFilter(
['abc', 'bcd', 'cde', 'def'],
function (val) {
return new Promise(function (rs) {
setTimeout(function() {
rs(val.indexOf('cd') !== -1);
}, 10);
});
},
function (err, res) {
console.log('promise callback:', err, res);
}
);
asyncFilter(
['abc', 'bcd', 'cde', 'def'],
function (val) {
return val.indexOf('cd') !== -1;
},
function (err, res) {
console.log('sync callback:', err, res);
}
);
output:
⌘ node example/filter.js
sync callback: null [ 'bcd', 'cde' ]
async callback: null [ 'bcd', 'cde' ]
promise callback: null [ 'bcd', 'cde' ]
Filter arr
using fn
,
and the results can be accessed by the callback done
.
Type: Array
The array to be filtered.
Type: Function
The filter function.
It can be synchronous,
with signature fn(val, index, arr)
.
If the returned value is truthy,
val
will be kept in the final results.
fn
can be made asynchronous if it does one of the following.
asyncFilter(
['abc', 'bcd', 'cde', 'def'],
function (val, i, arr, next) {
process.nextTick(function () {
next(null, val.indexOf('cd') !== -1);
});
},
function (err, res) {
console.log('async callback:', err, res);
}
);
asyncFilter(
['abc', 'bcd', 'cde', 'def'],
function (val) {
return new Promise(function (rs) {
setTimeout(function() {
rs(val.indexOf('cd') !== -1);
}, 10);
});
},
function (err, res) {
console.log('promise callback:', err, res);
}
);
Type: Function
Signature: done(err, results)
Called when all elements are checked.
If an error is thrown when executing fn
,
or the next
callback is passed a truthy value as the first argument,
or the returned promise rejects,
done
will be called immediately,
and filtering finishes.
FAQs
Async filter function for arrays
We found that array-async-filter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.