je n'étais pas vraiment satisfait de ce code:async parallèle avec rappel variadique
async.parallel([
getThing1,
getThing2,
getThing3
], function(err, responses) {
next.ifError(err);
var thing1 = responses[0],
thing2 = responses[1],
thing3 = responses[2];
// business
});
Je préfèrerais à ressembler à ceci, à la redemanderas
async.parallel([
getThing1,
getThing2,
getThing3
], function(err, thing1, thing2, thing3) {
next.ifError(err);
// business
});
C'est ce que je « ai fini avec:
async.variadic = function(fn) {
return function(err, responses) {
return fn.bind(this, err).apply(this, responses);
}
}
async.parallel([
getThing1,
getThing2,
getThing3
], async.variadic(function(err, thing1, thing2, thing3) {
next.ifError(err);
// business
}));
questions:
Est-ce que j'utilise correctement this
dans fn.bind(this, err).apply(this, responses);
?
Existe-t-il un moyen existant de le faire avec la bibliothèque asynchrone?
Mise à jour: Voici une autre façon d'y arriver:
async.variadic = (function() {
var _async = {};
_async.prototype = async;
_async.parallel = function(tasks, callback) {
return async.parallel(tasks, function(err, responses) {
return callback.bind(this, err).apply(this, responses);
});
};
return function() {
return _async;
};
})();
async.variadic().parallel([
getThing1,
getThing2,
getThing3
], function(err, thing1, thing2, thing3) {
});
I think I like this one most. Is this a good way to accomplish the task?
Voici une jsperf avec une autre idée: http://jsperf.com/variadic-async
Utiliser '.apply', au moins jusqu'à ce que ES6 est probablement votre meilleur et le seul pari pour convertir une fonction avec un param de tableau à une fonction ça prend des varargs. –