I use Meteor.wrapAsync to wrap my standard node.js function “sendMessage”.
And I use looping to call Meteor.wrapAsync and execute the sendMessageSync.
But then I got Error: “Error: Future resolved more than once”
Why I got that error ?
Here is my code:
//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.
}