我使用Meteor.wrapAsync来包装我的标准node.js函数" sendMessage"。
我使用循环来调用Meteor.wrapAsync并执行sendMessageSync。
但后来我得到了错误:"错误:未来解决了不止一次"
为什么我收到了这个错误?
这是我的代码:
//Every 10 seconds get messages and send.
Meteor.setInterval(function () {
var messages = getMessagesFromDb();
//Send message every two seconds.
Meteor.setInterval(sendAllMessages, 2000);
var i = 0;
function sendAllMessages() {
if (i++ < messages.length) {
//Get the sync version of sendMessage.
var sendMessageSync = Meteor.wrapAsync(sendMessage);
var result = sendMessageSync('is4ags', messages[i - 1]);
//Error: "Exception in callback of async function: Error: Future resolved more than once"
console.log('sendMessage response: ' + result);
}
}
}, 10000);
//sendMessage is standard node.js function that accept callback.
function sendMessage(toAddress, message, cb) {
//sendMessage Implementations.
}