Skip to content

Instantly share code, notes, and snippets.

@larzconwell
Last active Dec 20, 2015
Embed
What would you like to do?
var async = require('async');
function some (items, iterator) {
var ret = -1,
v;
for (var i in items) {
v = iterator(items[i]);
if (v) {
ret = i;
break;
}
}
return ret;
}
async.parallel([
function (done) {
done(null, null);
},
function (done) {
done(null, 'filePath');
},
function (done) {
done(null, null);
}
], function (err, results) {
if (err) {
throw err;
}
var idx = some(results, function (i) { return !!i; });
// => 1
if (idx === -1) {
throw new Error("File not found");
}
console.log('Found file', results[idx]);
// => 'filePath'
});
@techwraith
Copy link

techwraith commented Aug 5, 2013

function find(collection, filter) {
    for (var i = 0; i < collection.length; i++) {
        if(filter(collection[i], i, collection) return i;
    }
    return -1;
}

Does the same thing in a less confusing way :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment