How to use Promise.coroutine in node.js

Promise.coroutine is handy when it comes to you have multiple promises and one promise uses result from another promise.

Here is an example:

var sendEmailForUploadId = Promise.coroutine(function* (uploadId) {

var filemetas = yield Filemeta.find({ uploadId });
var bpInfo = yield BpInfo.findOne({ uploadId });
var info = { bpInfo, filemetas, HOST };
var template = yield generateTemplateWithInfo(info);

try {
yield sendEmail(template, sendToEmail);
} catch (e) {
throw e;
}

});

We use yield for function that return a promise. At the end, we have a try-catch block so that this function can propagate the error to the promise that use this whole function. I mean the function that use this promise can handle the error.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.