I want to save 8 objects to a MongoDB database using Mongoose. When the last document is saved, I want to inform (that is, send an event) that all documents have been saved.
The way I do it now is pretty messy (especially for the ever-increasing number of documents I want to keep).
Here is how I do it now (only for 4 people for this example). Is there a cleaner way you can recommend?
person1.save(function(err, result){ if (err) console.log(err); else{ person2.save(function(err, result){ if (err) console.log(err); else{ person3.save(function(err, result){ if (err) console.log(err); else{ person4.save(function(err, result){ if (err) console.log(err); else{ done(); } }); } }); } }); } });
source share