Error when using Meteor.wrapAsync. “Error: Future resolved more than once”

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.
}

This would typically happen if the sendMessage Node function you’re calling may fire the callback its passed more than once.

1 Like

I think your standard node function is using npm request. It returns both an error and a response in some instances. Either switch to Meteor’s HTTP.call() which is already synchronous or just copy the way the callback has been wrapped.

Yeah the error said that. Fiber.return called twice or more. But I don’t know where it happened.
I’ll check that. Thanks.

Yes, I use npm package request to send the message.

npm’s request is fine.
npm’s request will give error if there is some error or null.

My sendMessage code is something like this :

function sendMessage(toAddress, message, cb) {
    var requestOptions = { //some options };
    request(requestOptions, function (err, res, body) {
        if (err) {
            cb(err);
        }
        else {
            cb(null, res);
        }
    });
}

Is it Meteor bug or Fiber ?

Sometimes I got “Error: Future resolved more than once”.

I think Meteor or Fiber just throw error if there is some error from npm’s request, or return result.

So you think, the problem is with npm’s request.

OK, I’ll try to use Meteor’s HTTP.