Skip to content

Instantly share code, notes, and snippets.

@seanpmaxwell
Last active November 1, 2018 15:29
Show Gist options
  • Select an option

  • Save seanpmaxwell/868538f2da5c9d83c2db556bb718151c to your computer and use it in GitHub Desktop.

Select an option

Save seanpmaxwell/868538f2da5c9d83c2db556bb718151c to your computer and use it in GitHub Desktop.
/*************************************************
* Contact Dog Owner
************************************************/
async contactOwner(dogId: number, msg: string): Promise<void>
{
try {
let ownerEmail = await this._getOwnerEmail(dogId)
let info = await this._sendEmail(ownerEmail, msg)
console.log(info.message)
}
catch(err) {
console.error(err)
}
}
// .query() already returns a Promise
private _getOwnerEmail(dogId: number): Promise<string>
{
let sql = 'SELECT email FROM owners' +
'JOIN dogs' +
'ON owners.owner_id = dogs.owner_id' +
'AND dogs.dog_id = $1'
return db_.query(sql, [dogId])
}
// mailService.send() uses a callback, return custom Promise
private _sendEmail(email: string, msg): Promise<boolean>
{
return new Promise((resolve, reject) => {
mailService.send(email, msg, (err, info) => {
err ? reject(err) : resolve(info)
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment