【发布时间】:2017-05-30 12:09:42
【问题描述】:
我们正在使用 mailgun,我们的一些用户被列入黑名单。一次删除多个用户可能很乏味,所以我想通过 API 做同样的事情。我还想保存一些有关用户电子邮件被退回的原因的信息,以便我可以将其转发给他们。我这样做是这样的:
fs.readFile('emails.txt', 'utf8', function(err, data) {
if (err) throw err;
var emails = data.split(',');
async.eachSeries(emails, function(email, cb){
options.url = 'https://api.mailgun.net/v3/XXXXX.com/bounces/'+email
request.get(options, function(err, res, body){
body = JSON.parse(body);
error_code = body.error;
bounced_at = body.created_at;
var str = "\n Error code: "+body.error+" for email: "+email+" was bounced at: "+body.created_at
fs.appendFile("output.txt", str, function(err){
if(err) console.log(err)
console.log("Details were saved to output.txt")
})
});
request.del(options, function(err, res, body){
if(err){
cb(err);
} else {
console.log('Email: '+email+' has been unsuppressed');
}
})
cb(null);
}, function(err){
if(err){
console.log(err);
} else {
console.log('All bounced emails removed')
}
});
});
下面是一个有 5 个用户的测试。
输入:
test1@ah.com,test2@ah.com,test3@ah.com,test4@ah.com,test5@ah.com
输出:
Error code: 4.4.4 for email: test4@ah.com was bounced at: Tue, 30 May 2017 11:40:17 UTC
Error code: 3.3.3 for email: test3@ah.com was bounced at: Tue, 30 May 2017 11:40:05 UTC
Error code: 2.2.2 for email: test2@ah.com was bounced at: Tue, 30 May 2017 11:39:57 UTC
Error code: 1.1.1 for email: test1@ah.com was bounced at: Tue, 30 May 2017 11:39:45 UTC
Error code: 5.5.5 for email: test5@ah.com was bounced at: Tue, 30 May 2017 11:40:27 UTC
我们可以看到这些没有按顺序完成,这很好。当然这在异步文档here 中已经足够了。
请注意,由于此函数将 iteratee 并行应用于每个项目,因此无法保证 iteratee 函数将按顺序完成。
但是,当我用十封电子邮件进行测试时:
test1@ah.com,test2@ah.com,test3@ah.com,test4@ah.com,test5@ah.com,test6@ah.com,test7@ah.com,test7@ah.com,test8@ah.com,test9@ah.com,test10@ah.com
这是输出:
Error code: 2.2.2 for email: test2@ah.com was bounced at: Tue, 30 May 2017 11:43:57 UTC
Error code: undefined for email: test10@ah.com was bounced at: undefined
Error code: 1.1.1 for email: test1@ah.com was bounced at: Tue, 30 May 2017 11:43:50 UTC
Error code: 8.8.8 for email: test8@ah.com was bounced at: Tue, 30 May 2017 11:44:43 UTC
Error code: 3.3.3 for email: test3@ah.com was bounced at: Tue, 30 May 2017 11:44:05 UTC
Error code: 6.6.6 for email: test6@ah.com was bounced at: Tue, 30 May 2017 11:44:28 UTC
Error code: 5.5.5 for email: test5@ah.com was bounced at: Tue, 30 May 2017 11:44:21 UTC
Error code: 4.4.4 for email: test4@ah.com was bounced at: Tue, 30 May 2017 11:44:13 UTC
Error code: undefined for email: test7@ah.com was bounced at: undefined
Error code: 9.9.9 for email: test9@ah.com was bounced at: Tue, 30 May 2017 11:44:55 UTC
似乎在某些情况下,我的request.get() 是在request.del() 之后执行的 -> 这导致输出日志在某些地方具有Undefined。如何确保始终先执行request.get()?
【问题讨论】:
-
摆脱回调,使用承诺。
-
@loan 你能证明这一点,我很乐意接受答案。
标签: javascript node.js asynchronous