Async.auto, but with a collection of objects. autoMap is useful when you need to asynchronously fetch an array of objects, apply some complex flowchart of events to each object, and then report back after every object is done processing.
autoMap(
(next) => {
entity.find({
type: 'character',
status: 'alive',
}).toArray(next);
},
concurrency,
{
isImmune(item, done) {
if (item.immunities.indexOf(damageType) > -1){
return done(null, true);
}
return done(null, false);
},
madeSavingThrow(done) {
if (character.savingThrow > Math.random()) {
return done(null, true);
}
return done(null, false);
},
newHitPoints(isImmune, madeSavingThrow, done) {
if (!madeSavingThrow && !isImmune) {
return done(null, character.hitPoints - Math.random() * 10);
}
return done();
}],
update(newHitPoints, done) {
if (newHitPoints !== undefined) {
character.hitPoints = newHitPoints;
if (character.hitPoints < 0) {
character.status = 'dead';
}
return character.update(done);
}
return done();
},
(character, results) => {
const event = {
type: 'attacked',
character
};
if (results.newHitPoints === undefined) {
event.result = 'miss';
} else if (results.newHitPoints > 0) {
event.result = 'hit';
} else {
event.result = 'death';
}
return event;
},
(err, allEvents) => {
allEvents.forEach( (event) => {
playEventAnimation(event);
});
});
});