Sending the Email

With the infrastructure bits in place, we can move onto actually sending the email. Let’s write the handler for the Send command:

 function​ createHandlers ({
  justSendIt,
  messageStore,
  systemSenderEmailAddress
 }) {
 return​ {
  Send: command => {
 const​ context = {
  messageStore,
  justSendIt,
  systemSenderEmailAddress,
  sendCommand: command
  }
 
 return​ Bluebird.resolve(context)
  .then(loadEmail)
  .then(ensureEmailHasNotBeenSent)
  .then(sendEmail)
  .then(writeSentEvent)
 // If it's already sent, then we do a no-op
  .​catch​(AlreadySentError, () => {})
  .​catch​(
  SendError,
  err => writeFailedEvent(context, err)
  )
  }
  }
 }

It uses a Promise ...

Get Practical Microservices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.