How to update the value of a variable within a callback?

Asked

Viewed 828 times

1

I am creating a module with Nodejs and in this module I created a function that returns a value, being this value updated within a function of type callback. The problem is that this variable/value is not being updated. I would like to know how to solve the problem. I’m a beginner in Nodejs and I researched it, but I couldn’t find the solution to my problem. Below the code, the variable I refer to is "output", declared at the beginning of the function:

exports.sendMessage = function (chat, message, workspaceId){

    let output = "";

    chat.message(
        {
            input: { text: message },
            workspace_id: workspaceId
        },
        function (error, response){
            if(error){
                console.error(error);
            } else {
                output = response;
                console.log(JSON.stringify(response, null, 2));
            }
        }
    );

    return output;
}

1 answer

0


You can’t use returns for this. When the function sendMessage returns, the function you associated with chat.message may not even have run yet, because all this is asynchronous.

There where you did output = response, you should remove this and make a function call that will continue the work for you, because only there is that you are sure that has the value of response correct.

For example:

processSendMessageResponse = function(response) {
    // fazer alguma coisa com a resposta.
}

exports.sendMessage = function (chat, message, workspaceId){

    let output = "";

    chat.message(
        {
            input: { text: message },
            workspace_id: workspaceId
        },
        function (error, response, output){
            if(error){
                console.error(error);
            } else {
                processSendMessageResponse(response)
                console.log(JSON.stringify(response, null, 2));
            }
        }
    );

}

Another option is the function sendMessage receive by parameter, along with the three it already receives, the function itself that will be called when it ends. For example:

exports.sendMessage = function (chat, message, workspaceId, messageSentCallback){

    let output = "";

    chat.message(
        {
            input: { text: message },
            workspace_id: workspaceId
        },
        function (error, response, output){
            if(error){
                console.error(error);
            } else {
                messageSentCallback(response);
                console.log(JSON.stringify(response, null, 2));
            }
        }
    );

}

and the use of it could be so:

processSendMessageResponse = function(response) {
    // fazer alguma coisa com a resposta.
}

sendMessage(chat, message, workspaceId, processSendMessageResponse);

or, even simpler:

sendMessage(chat, message, workspaceId, function(response) {
    // fazer alguma coisa com a resposta.
});

I recommend you read more about asynchronous functions to get the paradigm right, as it is essential to work well with Nodejs and Javascript in general.

  • I understand what you mean. But is there any way that I could return this exit to another place? because I am using the sendMessage function in another/script file, there I just make the function call and wait for the return to process the answer. If I process the answer with another function like you did in callback’s 'Else', is there a way to return that answer to that other file I’m using? I thank you in advance for your patience.

  • You will need a function there in the other file that is visible from that, similar to what I did in the reply, but there in the other file. Or you can do the function sendMessage receive as parameter the function itself to be called. I edited the answer with this solution.

  • I get it. I’ll try to do that and see if it solves my problem. Thank you very much for answering and for your patience. I am a complete beginner in Node and need to do a project using this tool. I ended up learning a lot from your answer. Thank you very much, I will mark it as correct!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.