【发布时间】:2016-07-20 02:26:58
【问题描述】:
我有一个定义某些方法的 Meteor 项目。在那些中,发送电子邮件。由于这需要大量时间并且会减慢最终用户的整个操作速度,因此我想异步发送电子邮件。邮件发送成功与否对方法调用的结果没有影响。
据我发现,使用setTimeout 模拟异步调用很常见。在这种情况下我也应该这样做吗?
编辑: cmets 中要求的代码
export const UpdateMaterial = new ValidatedMethod({
name: 'material.update',
validate: new SimpleSchema({
id: {type: String},
description: {type: String},
}).validator(),
run({id, description}){
const _id = new Meteor.Collection.ObjectID(id);
let res;
if(Meteor.isServer) {
res = Materials.update({_id}, {
$set: {
'metadata.description': description
}
});
}
SendHTMLEmailToRoles(Titles.NewMaterial,Texts.NewMaterial, [Roles.Admin]);
return res;
}
});
export const SendHTMLEmailToRoles = (subject,html,roles) => {
if(Meteor.isServer) {
const users = Meteor.users.find({role: {$in: roles}}).fetch();
const addresses = users.map(function(user){
if(!user.emails)
return;
return user.emails.pop().address;
});
Email.send({
to: addresses,
from: 'test@test.com',
subject,
html
});
}
}
【问题讨论】:
-
你的代码是什么?
-
@epascarello 编辑了问题
-
@SergioTulentsev 我知道如何发送电子邮件,异步部分是问题
-
好吧,那里的文档说“异步发送电子邮件”。
标签: javascript jquery asynchronous meteor