I have a rest api where a user gets added to an organization. First the user is added to the organization and then an email is sent. When the email is sent a 200 response is returned. Problem is it takes a few seconds for the mail to be send. So the user has to wait for a few seconds. Is there anyway to send the 200 response right after the user is added and then the email is sent "in the background". I using nodemailer.
Some pseudo code:
organization.members.push({user:createdUser._id, tier:4, joinedDate:new Date()});
organization.save(function(){
callback.send(200, 'OK') //If I do this, the code below is not executed.
sendMail(function(){
console.log('Mail has been sent')
}
}