Skip to content

Instantly share code, notes, and snippets.

@jacobgardner
Created February 8, 2017 05:46
Show Gist options
  • Save jacobgardner/32c231a8ceecbf24a1d63a29b7dd21e8 to your computer and use it in GitHub Desktop.
Save jacobgardner/32c231a8ceecbf24a1d63a29b7dd21e8 to your computer and use it in GitHub Desktop.
// mapAsync
function mapAsync(arr, process, callback) {
var output = [];
arr.forEach((element) => {
process(element, (err, data) => {
if (err) {
throw err;
}
output.push(data);
if (output.length === arr.length) {
callback(output);
}
});
});
}
function yourCode() {
mapAsync(vehicles, (vehicle, callback) => {
VehicleModel.findOne({_id: ownedVehicle.vehicleId}, (err, vehicle) => {
if (err) callback(err);
console.log(vehicle);
callback(null, {
year: vehicle.year,
make: vehicle.make,
model: vehicle.model,
});
});
}, (output) => {
// This is called when all the shit is computered
res.json(output);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment