Created
February 8, 2017 05:46
-
-
Save jacobgardner/32c231a8ceecbf24a1d63a29b7dd21e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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