Skip to content

Instantly share code, notes, and snippets.

@larzconwell
Last active December 20, 2015 15:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save larzconwell/6158099 to your computer and use it in GitHub Desktop.
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

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