【发布时间】:2016-03-16 12:47:20
【问题描述】:
在我正在创建的应用程序中,用户将能够将对象的描述发送给多个收件人(从 1 到 200)。使用 Parse Cloud Code 我将不得不使用 promise 来等待电子邮件服务器对每封电子邮件的响应(Mailgun)。
有没有其他方法可以将所有这些创建的电子邮件存储在某种数组中,然后一次性发送到电子邮件服务器?否则我会达到一个函数可以运行的最大 15 秒。
现在我用这个:
Parse.Cloud.define("emailobject", function(request, response) {
// ... some company info parameters
var theTraders = request.params.traders;
var objectQ = new Parse.Query("objects");
objectQ.equalTo("objectId", theObjectId);
objectQ.first({
success:function(theObject) {
// ... more code
searchObjectPictures(theObject, {
success:function(pictureObjects) {
for (var a = 0; a < theTraders.length; a++) {
// create the parameters to create the email
var mailingParameters = {};
// ... create the parameters
// when the email html has been compiled
mailgun.sendWelcomeEmail({
to: traderEmail,
from: toString,
subject: subjectString,
html: mailing.getMailingHTML(mailingParameters)
}, {
success:function(httpResponse) {
console.log(httpResponse);
},
error:function(httpResponse) {
console.log(httpResponse);
}
});
}
// emailedObjectsToSave is an array of analytics objects
Parse.Object.saveAll(emailedObjectsToSave, {
success:function(list) {
response.success(true);
},
error:function(error) {
response.error(error);
}
});
},
error:function(error) {
response.error(error);
}
});
},
error:function(error){
response.error(error);
}
});
});
我知道对于嵌套查询来说,promise 会更好,但我还在纠结这个问题。
谢谢
【问题讨论】:
-
键是
Parse.Promise.when(),它接受一个promise数组并返回一个promise,当所有传递的promise都被履行时,这个promise就会被履行。让您的 maligun 函数回答一个承诺(不提供完成处理程序),在循环中调用它并用这些承诺填充一个数组。 -
@danh 我会试试的。谢谢!对超时有任何想法吗?
-
唯一的选择是以短频率运行并执行较小批次的计划作业,从上次运行停止的地方开始。