vendredi 2 octobre 2015

node.js async each random processiong

I have a async.each loop that runs perfect for my task, but returns always the same result:

The following code should return onMatch(2) or onMatch(4) in a random Order but it returns always a onMatch(2).

How I get the requested result without a timeout/delay. It should run as fast as possible.

var arr = {a: '1', b: '2', c: '3', d: '4', e: '5'}
var async = require('async')

function matchPattern(pat, object, cb){
    if(pat % object == 0){
        return cb(pat);
    } else{
        return cb();
    }
}

function matchingPattern(patterns, object, onMatch, onNoMatch) {
    async.each(patterns, function(pat, cb){
        matchPattern(pat, object, function(match){
            return cb(match);
        });
    }, function (match){
        if(match) return onMatch(match);
        return onNoMatch()
    });
}

matchingPattern(arr, 2, function onMatch(a){
    console.log('onMatch('+a+')');
}, function onNoMatch(){
    console.log('onNoMatch()');
});

res.sendStatus(200);




Aucun commentaire:

Enregistrer un commentaire