[英]how to use asynchronous and synchronous programming in right time
我是JavaScript異步編程的新手。 我有兩個電子郵件提供商sendgrid和mailgun。 我想使用其中之一發送電子郵件,如果發生任何錯誤,請重新發送一封電子郵件。 最后,如果成功發送的電子郵件保存到db中,並使用json對象進行響應。 這是我的代碼:
if(req.query.provider== 'mailgun'){
console.log('sending with mailgun ...');
mailgun.messages().send(data, function (err, res) {
if(err){
fail = true;
console.log(`error in sending email with mailgun. error :${err}`)
} else {
console.log('email sent with mailgun')
fail = false;
}
});
}else if(req.query.provider == 'sendgrid'){
console.log('sending emial with sendgrid')
sendgrid.sendMail(data, function(err, res) {
if (err) {
console.log(`error in sending with sendgrid. error: ${err} + response : ${res}`)
fail = true
}
console.log(`email sent with sendgrid`)
fail = false
});
}
if(fail){
if(req.query.provider == 'mailgun'){
console.log('sending with sendgrid ...')
sendgrid.sendMail(data, function(err, res){
if(err){
console.log(`error: ${err} + response : ${res}`)
}else {
console.log(`response: ${res}`)
fail = false
}
})
}else if(req.query.provider == 'sendgrid'){
console.log('sendging with mailgun')
mailgun.messages().send(data, function (err, res) {
if(err){
console.log(`error: ${err} + response : ${res}`)
}else{
console.log(`response: ${res}`)
fail = false
}
})
}
}
if(!fail){
db.models.Email.create(req.query, (err, result)=>{
if(err){
handleErrors(res, err);
console.log(`error in creating Email :${err}`)
}else {
console.log()
res.json(result)
}
});
} else {
console.log('fail in sending error');
res.json('sorry.');
}
});
這里的問題是,此代碼異步運行。 例如,它使用mailgun發送,然后跳轉到失敗檢查並再次使用sendgrid發送。 不等待發送的響應。 我該如何解決和改進呢? 我應該使用async',
await'嗎?
這是使用異步/等待模式的示例
(可以改進以支持一般的多個郵件程序,您必須修改選擇“ alternativeMailerName”的部分) 。
/**
* Send an email using a given mailer
*
* return true if success, false otherwise
*/
async sendMailUsingMessages(mailerName, data) {
const {
mailer,
func,
} = [
{
mailer: 'mailgun',
func: async () => mailgun.messages().send(data);
},
{
mailer: 'sendgrid',
func: async () => sendgrid.sendMail(data);
},
].find(x => x.mailer === mailerName);
console.log(`sending with ${mailer} ...`);
try {
const res = await func();
console.log(`email sent with ${mailer}`);
return true;
} catch (err) {
console.log(`error in sending email with ${mailer}. error :${err}`)
return false;
}
}
/**
* Send an email
*/
async sendEmail(req, res, data) {
if (!(await sendMailUsingMessages(req.query.provider, data))) {
// One fail, so we try the other one
const alternativeMailerName = req.query.provider === 'mailgun' ? 'sendgrid' : 'mailgun';
if (!(await sendMailUsingMessages(alternativeMailerName, data))) {
// Double fail
console.log('fail in sending error');
res.json('sorry.');
return;
}
}
// Success
try {
const result = await db.models.Email.create(req.query);
res.json(result);
} catch (err) {
handleErrors(res, err);
console.log(`error in creating Email :${err}`)
}
}
您可以嘗試推遲並承諾執行JavaScript。 請不要延遲和承諾下面的第一個示例。 之后,您會發現延期和承諾執行有多么容易,
var firstData = null; var secondData = null; var responseCallback = function () { if (!firstData || !secondData) return; // do something } $.get("http://example.com/first", function (data) { firstData = data; responseCallback(); }); $.get("http://example.com/second", function (data) { secondData = data; responseCallback(); })
Then the same with deferred and promise,
var firstPromise = $.get("http://example.com/first"); var secondPromise = $.get("http://example.com/second"); $.when(firstPromise, secondPromise).done(function (firstData, secondData) { // do something });
您可以在這里閱讀更多內容https://davidwalsh.name/write-javascript-promises
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.