Last active
November 1, 2018 15:29
-
-
Save seanpmaxwell/868538f2da5c9d83c2db556bb718151c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /************************************************* | |
| * 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