Callback as argument return

1

My question is as follows, so I understand callbacks are functions passed as a parameter that will be executed when any statement is performed, but I often see callback as reserved word to return parameters to a function, such as in the example below:

function run(textQuery, callback)
{
    const sql = require('mssql');
    var callbackDone = false;    
    const connectionPool = new sql.ConnectionPool(config, err => {   
        connectionPool.request()
        .query(textQuery, (err, result) => {            
            sql.close();
            callbackDone = true;
            callback(err, result);
        })    
    })    
    connectionPool.on('error', err => {        
        sql.close();
        if(!callbackDone)
        {
            callback(err, null);
        }
    })
}   

Could you explain how this works?

    
asked by anonymous 08.01.2018 / 18:59

1 answer

0

This covers a lot about the concept of callback . The difference to what you scored is that you can pass the callback as a function parameter when the return function needs additional parameters (most of the times a missing value). I'll illustrate:

function handler(err, res, message){
    // message é o parametro que estava faltando
    console.log(err, res, message);
}

function run(textQuery, callback){
    callback(null, [], "Success");
}

run("", handler);
    
08.01.2018 / 19:50