Created
October 20, 2017 21:30
-
-
Save ghmeier/1809b25d1c275833605a3f7cb9f5366d to your computer and use it in GitHub Desktop.
Promise Patterns (and tools) at Mixmax.
This file contains 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
/** | |
* Async/Await | |
*/ | |
const mongoist = require('mongoist'); | |
const db = mongoist('mongodb://user:password@mongo.net'); | |
function getContact(name) { | |
return db.contacts.findOne({ | |
firstName: name | |
}); | |
} | |
const got = require('got'); | |
// Instructions for getting this token are here: | |
// https://developer.mixmax.com/docs/getting-started-with-the-api | |
const MIXMAX_API_TOKEN = 'test_token'; | |
async function sendEmail(name, message) { | |
const contact = await getContact(name); | |
const { email } = contact; | |
const message = { | |
to: email, | |
subject: 'Promises are better.' | |
html: message | |
} | |
const result = await got({ | |
url: 'https://api.mixmax.com/v1/send', | |
method: 'POST',\ | |
headers: { | |
'X-API-TOKEN': MIXMAX_API_TOKEN | |
} | |
body: JSON.stringify({ message }) | |
}); | |
return result; | |
} | |
async function sendManyEmails(names, message) { | |
try { | |
const results = await Promise.all(names.map((name) => { | |
return sendEmail(name, message); | |
}); | |
// Handle results. | |
} catch (err) { | |
// Handle error. | |
} | |
} |
This file contains 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
/** | |
* Callbacks | |
*/ | |
const mongojs = require('mongojs'); | |
const db = mongojs('mongodb://user:password@mongo.net'); | |
function getContact(name, cb) { | |
db.contacts.findOne({ | |
firstName: name | |
}, (err, res) => { | |
if (err) return cb(err); | |
cb(null, res); | |
}); | |
} | |
const request = require('request'); | |
// Instructions for getting this token are here: | |
// https://developer.mixmax.com/docs/getting-started-with-the-api | |
const MIXMAX_API_TOKEN = 'test_token'; | |
function sendEmail(name, message, cb) { | |
getContact(name, (err, result) => { | |
if (err) return cb(err); | |
const { email } = result; | |
const message = { | |
to: email, | |
subject: 'Callbacks are ok.' | |
html: message | |
} | |
request({ | |
url: 'https://api.mixmax.com/v1/send', | |
method: 'POST',\ | |
headers: { | |
'X-API-TOKEN': MIXMAX_API_TOKEN | |
} | |
body: JSON.stringify({ message }) | |
}, (err, result) => { | |
cb(err, result); | |
}); | |
}); | |
} | |
function sendManyEmails(names, message, cb) { | |
names.each((name) => { | |
sendEmail(name, message, (err, result) => { | |
// only one is finished. | |
}); | |
}); | |
// How do I know when they all finish? | |
} |
This file contains 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
/** | |
* Promises | |
*/ | |
const mongojs = require('mongojs'); | |
const db = mongojs('mongodb://user:password@mongo.net'); | |
function getContact(name) { | |
return new Promise((resolve, reject) =>{ | |
db.contacts.findOne({ | |
firstName: name | |
}, (err, res) => { | |
if (err) return reject(err); | |
resolve(res); | |
}); | |
}); | |
} | |
const request = require('request'); | |
// Instructions for getting this token are here: | |
// https://developer.mixmax.com/docs/getting-started-with-the-api | |
const MIXMAX_API_TOKEN = 'test_token'; | |
function sendEmail(name, message) { | |
return getContact(name).then((result) => { | |
const { email } = result; | |
const message = { | |
to: email, | |
subject: 'Promises are better.' | |
html: message | |
} | |
return new Promise((resolve, reject) => { | |
request({ | |
url: 'https://api.mixmax.com/v1/send', | |
method: 'POST',\ | |
headers: { | |
'X-API-TOKEN': MIXMAX_API_TOKEN | |
} | |
body: JSON.stringify({ message }) | |
}, (err, result) => { | |
if (err) return reject(err); | |
resolve(result); | |
}); | |
}); | |
}).then((result) => { | |
return Promise.resolve(result); | |
}); | |
} | |
function sendManyEmails(names, message) { | |
Promise.all(names.map((name) => { | |
return sendEmail(name, message); | |
}).then((results) => { | |
//Handle the array. | |
}).catch((err) => { | |
// Handle the error. | |
}) | |
} |
This file contains 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
/** | |
* Using promise-callbacks | |
* More at: github.com/mixmaxhq/promise-callbacks | |
*/ | |
const { deferred } = require('promise-callbacks'); | |
function getContact(name) { | |
const promise = deferred(); | |
db.contacts.findOne({ | |
firstName: name | |
}, promise.defer()); | |
return promise; | |
} | |
/** | |
* Using promise-pool | |
* More at: github.com/mixmaxhq/promise-pool | |
*/ | |
const PromisePool = require('promise-pool'); | |
async function bulkSendEmails(names) { | |
const pool = new PromisePool(10); | |
names.each((name) => { | |
await pool.start(async () => { | |
sendEmail(name, 'Hello!'); | |
}); | |
}); | |
await pool.flush(); | |
return { message: 'success' }; | |
} | |
/** | |
* Using promise-iterate | |
* More at: github.com/mixmaxhq/promise-iterate | |
*/ | |
const { promiseIterate } = require('promise-iterate'); | |
const mongoist = require('mongoist'); | |
const db = mongoist('mongodb://user:password@mongo.net'); | |
async function sendToEveryone(names) { | |
const cursor = db.contacts.findAsCursor({}); | |
await promiseIterate(cursor, async (contact) => { | |
await sendEmail(contact.firstName, 'Hello!'); | |
}); | |
return { message: 'success' }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment