Examples referenced in async js blog post
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
// `series` Method Example | |
function eatFreeBread (bread, callback) { | |
setTimeout(function () { | |
console.log('Just ate some ' + bread + ' bread'); | |
callback(null, {breadRating: 8}); | |
}, 3000); | |
} | |
function eatAppetizers (apps, callback) { | |
setTimeout(function () { | |
console.log('Just ate these great appetizers ' + apps); | |
callback(null, {appRating: 9}); | |
}, 3000); | |
} | |
function eatMainCourse (mainCourse, callback) { | |
setTimeout(function () { | |
console.log('Just ate the ' + mainCourse + ' main course'); | |
callback(null, {mainCourseRating: 9}); | |
}, 3000); | |
} | |
function eatDessert (dessert, callback) { | |
setTimeout(function () { | |
console.log('Just ate the ' + dessert + ' dessert'); | |
callback(null, {dessertRating: 10}); | |
}, 3000); | |
} | |
function handlePoorService () { | |
console.log('WE ARE DONE WITH THIS NON SENSE AND ARE LEAVING!') | |
} | |
function goOutToEatV1 (bread, apps, mainCourse, dessert, restaurantReviewCallback) { | |
eatFreeBread(bread, function (err, breadResult) { | |
if (err) { | |
handlePoorService(); | |
restaurantReviewCallback(err); | |
} else { | |
eatAppetizers(apps, function (err, appsResult) { | |
if (err) { | |
handlePoorService(); | |
restaurantReviewCallback(err); | |
} else { | |
eatMainCourse(mainCourse, function (err, mainCourseResult) { | |
if (err) { | |
handlePoorService(); | |
restaurantReviewCallback(err); | |
} else { | |
eatDessert(dessert, function (err, dessertResult) { | |
if (err) { | |
handlePoorService(); | |
restaurantReviewCallback(err); | |
} else { | |
restaurantReviewCallback(null, [ | |
breadResult, | |
appsResult, | |
mainCourseResult, | |
dessertResult | |
]); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
} | |
function goOutToEatV2 (bread, apps, mainCourse, dessert, restaurantReviewCallback) { | |
async.series([ | |
function (callback) { | |
eatFreeBread(bread, callback); | |
}, | |
function (callback) { | |
eatAppetizers(apps, callback); | |
}, | |
function (callback) { | |
eatMainCourse(mainCourse, callback); | |
}, | |
function (callback) { | |
eatDessert(dessert, callback); | |
}, | |
], function (err, restaurantResults) { | |
if (err) { | |
handlePoorService(); | |
} | |
restaurantReviewCallback(err, restaurantResults); | |
}) | |
} | |
function goOutToEatV3 (bread, apps, mainCourse, dessert, restaurantReviewCallback) { | |
async.series([ | |
_.curry(eatFreeBread)(bread), | |
_.curry(eatAppetizers)(apps), | |
_.curry(eatMainCourse)(mainCourse), | |
_.curry(eatDessert)(dessert), | |
], function (err, restaurantResults) { | |
if (err) { | |
handlePoorService(); | |
} | |
restaurantReviewCallback(err, restaurantResults); | |
}) | |
} | |
const bread = 'garlic'; | |
const apps = ['rockefeller oysters', 'spinach and artichoke dip', 'bruschetta']; | |
const mainCourse = 'filet mignon'; | |
const dessert = 'cheesecake'; | |
const restaurantReviewCallback = function (err, results) { console.log('done with our review! ' + JSON.stringify(results)); }; | |
// Uncomment to run different versions of the solution | |
// goOutToEatV1(bread, apps, mainCourse, dessert, restaurantReviewCallback); | |
// goOutToEatV2(bread, apps, mainCourse, dessert, restaurantReviewCallback); | |
// goOutToEatV3(bread, apps, mainCourse, dessert, restaurantReviewCallback); | |
// `each` Method Example | |
function sendEmail (emailAddress, email, callback) { | |
setTimeout(function () { | |
console.log('Sent the following email ' + email + ' to the following email address ' + emailAddress); | |
callback(null, {emailId: Math.floor(Math.random() * 10000)}); | |
}, 3000); | |
} | |
function sendBatchOfEmailsV1 (emailAddresses, emailBody, callback) { | |
var emailHasFailed = false, | |
emailAddressIdx = 0, | |
emailIds = []; | |
while (!emailHasFailed && emailAddressIdx < emailAddresses.length) { | |
sendEmail(emailBody, emailAddresses[emailAddressIdx], function (err, result) { | |
if (err) { | |
emailHasFailed = true; | |
return callback(err); | |
} else { | |
emailIds.push(result.emailId); | |
} | |
if (emailIds.length === emailAddresses.length) { | |
callback(null, emailIds); | |
} | |
}); | |
emailAddressIdx += 1; | |
} | |
} | |
function sendBatchOfEmailsV2 (emailAddresses, emailBody, callback) { | |
async.map(emailAddresses, function (emailAddress, asyncCallback) { | |
sendEmail(emailBody, emailAddress, asyncCallback); | |
}, callback); | |
} | |
function sendBatchOfEmailsV3 (emailAddresses, emailBody, batchCallback) { | |
async.map(emailAddresses, _.curry(sendEmail)(email), batchCallback); | |
} | |
const email = 'Async.js Hello World!'; | |
const emailAddresses = [ | |
'jdoe0@gmail.com', | |
'jdoe1@gmail.com', | |
'jdoe2@gmail.com', | |
'jdoe3@gmail.com', | |
'jdoe4@gmail.com', | |
'jdoe5@gmail.com', | |
'jdoe6@gmail.com', | |
'jdoe7@gmail.com', | |
'jdoe8@gmail.com', | |
'jdoe9@gmail.com' | |
]; | |
const batchEmailCallback = function (err, emailIds) { console.log('Batch email send result = ' + JSON.stringify(emailIds)); }; | |
// Uncomment to run different versions of the solution | |
// sendBatchOfEmailsV1(emailAddresses, email, batchEmailCallback); | |
// sendBatchOfEmailsV2(emailAddresses, email, batchEmailCallback); | |
// sendBatchOfEmailsV3(emailAddresses, email, batchEmailCallback); | |
// `waterfall` Method Example | |
function makeReservation (reservationDetails, callback) { | |
setTimeout(function () { | |
console.log('Made the following reservation ' + JSON.stringify(reservationDetails)); | |
callback(null, Math.floor(Math.random() * 10000)); | |
}, 3000); | |
} | |
function processCreditCardForReservation (creditCardDetails, reservationId, callback) { | |
setTimeout(function () { | |
console.log('Charged the following reservation ' + reservationId + ' to the following card ' + JSON.stringify(creditCardDetails)); | |
callback(null, Math.floor(Math.random() * 10000)); | |
}, 3000); | |
} | |
function emailAirlineTicketReceipt (emailAddress, receiptId, callback) { | |
setTimeout(function () { | |
console.log('Emailed a receipt for receipt ID ' + receiptId + ' to the following email address ' + emailAddress); | |
callback(null, Math.floor(Math.random() * 10000)); | |
}, 3000); | |
} | |
function bookAirlineTicketV1 (reservationDetails, creditCardDetails, receiptEmailAddress, bookAirlineTicketCallback) { | |
makeReservation(reservationDetails, function (reservationErr, reservationId) { | |
if (reservationErr) { | |
bookAirlineTicketCallback(reservationErr); | |
} else { | |
processCreditCardForReservation(creditCardDetails, reservationId, function (creditCardProcessingError, receiptId) { | |
if (creditCardProcessingError) { | |
bookAirlineTicketCallback(creditCardProcessingError); | |
} else { | |
emailAirlineTicketReceipt(receiptEmailAddress, receiptId, function (emailError, emailId) { | |
bookAirlineTicketCallback(emailError); | |
}); | |
} | |
}); | |
} | |
}); | |
} | |
function bookAirlineTicketV2 (reservationDetails, creditCardDetails, receiptEmailAddress, bookAirlineTicketCallback) { | |
async.waterfall([ | |
function(callback) { | |
makeReservation(reservationDetails, callback); | |
}, | |
function(reservationId, callback) { | |
processCreditCardForReservation(creditCardDetails, reservationId, callback); | |
}, | |
function(receiptId, callback) { | |
emailAirlineTicketReceipt(receiptEmailAddress, receiptId, callback); | |
} | |
], bookAirlineTicketCallback); | |
} | |
function bookAirlineTicketV3 (reservationDetails, creditCardDetails, receiptEmailAddress, bookAirlineTicketCallback) { | |
async.waterfall([ | |
_.curry(makeReservation)(reservationDetails), | |
_.curry(processCreditCardForReservation)(creditCardDetails), | |
_.curry(emailAirlineTicketReceipt)(receiptEmailAddress), | |
], bookAirlineTicketCallback); | |
} | |
const reservationDetails = { destination: 'Cancun, Mexico', departureDate: '05/05/2018', airline: 'Delta' }; | |
const creditCardDetails = { number: '1111111111111111', cvv: '123', zipcode: '12345' }; | |
const receiptEmailAddress = 'jdoe@gmail.com'; | |
const bookingCallback = function (err, results) { console.log('Flight has been booked!'); }; | |
// Uncomment to run different versions of the solution | |
// bookAirlineTicketV1(reservationDetails, creditCardDetails, receiptEmailAddress, bookingCallback); | |
// bookAirlineTicketV2(reservationDetails, creditCardDetails, receiptEmailAddress, bookingCallback); | |
// bookAirlineTicketV3(reservationDetails, creditCardDetails, receiptEmailAddress, bookingCallback); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment