- Source:
Examples
// array
var order = [];
var array = [1, 3, 2];
var iterator = function(num, done) {
setTimeout(function() {
order.push(num);
done(null, num % 2);
}, num * 10);
};
async.detect(array, iterator, function(err, res) {
console.log(res); // 1
console.log(order); // [1]
});
// array with index
var order = [];
var array = [1, 3, 2];
var iterator = function(num, index, done) {
setTimeout(function() {
order.push([num, index]);
done(null, num % 2);
}, num * 10);
};
async.detect(array, iterator, function(err, res) {
console.log(res); // 1
console.log(order); // [[1, 0]]
});
// object
var order = [];
var object = { a: 1, b: 3, c: 2 };
var iterator = function(num, done) {
setTimeout(function() {
order.push(num);
done(null, num % 2);
}, num * 10);
};
async.detect(object, iterator, function(err, res) {
console.log(res); // 1
console.log(order); // [1]
});
// object with key
var order = [];
var object = { a: 1, b: 3, c: 2 };
var iterator = function(num, key, done) {
setTimeout(function() {
order.push([num, key]);
done(null, num % 2);
}, num * 10);
};
async.detect(object, iterator, function(err, res) {
console.log(res); // 1
console.log(order); // [[1, 'a']]
});