Вот, что в итоге получилось:
/**
* Event-style map function with fun(e, consumer) as an element
* transformation function that does not return modified object
* but produce it to the ''consumer'' function instead.
*
* Result of the evmap-transformed objects will be transfered in
* the ''callback'' call eventually.
*
* Main use is for maps with asynchronous transformation functions!
*/
function evmap(list, fun, callback) {
function evmap(acc, list, fun) {
if (list && 0 < list.length) {
fun(list.shift(),
function (result) {
acc.unshift(result);
evmap(acc, list, fun);
});
}
else {
callback(acc.reverse());
}
}
evmap([], list, fun);
}
//// unit tests
exports.evmap_array = {
'list': function (test) {
evmap([1,2,3],
function (e, c) { c(e + 10); },
function (result) {
test.deepEqual(result, [11, 12, 13]);
test.done();
});
}
};