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

1

I am creating a module with NodeJS and in this module I created a function that returns a value, this value being updated within a callback function. The problem is that this variable / value is not being updated. I would like to know how to solve the problem. I am a beginner in NodeJS and I researched it, but I did not find the solution to my problem. Here is the code, the variable I mean is the "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;
}
    
asked by anonymous 07.10.2017 / 04:56

1 answer

0

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

Where you have done output = response , you should remove this and make a function call that will continue the work for you, since it is only there that you are sure that it has the value of response correct. p>

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 sendMessage function to receive per parameter, along with the three that it already receives, the very function that will be called when it finishes. 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 its use might look like this:

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

sendMessage(chat, message, workspaceId, processSendMessageResponse);

or, simpler still:

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

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

    
07.10.2017 / 05:30