简体   繁体   中英

How to call multiple API synchronise in Node.js

Below is the code I have,

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;

var config = {
    userName: 'XXXX',
    password: 'XXXX',
    server: 'XXXX',
    options: {
        database: 'XXXX',
        encrypt: true
    }
}

var connection = new Connection(config);

connection.on('connect', function (err) {
    if (err) {
        console.log(err);
    }
    else {
        // // queryDatabase();
    }
}
);

There is huge chaining in my code because of asynchronize execution of code. I found some libraries , but don't think to use them for production purpose.

How to call 'connect' synchronize and continue sequential flow of the code? What is the best way to achieve sequential execution of the code in Node.js ?

would something like this work ?

var config = {
    userName: 'XXXX',
    password: 'XXXX',
    server: 'XXXX',
    options: {
        database: 'XXXX',
        encrypt: true
    }
}

var connection = new Connection(config);

function isConnected(){
    return new Promise(function(resolve, reject){
            connection.on('connect', function (err) {
            if (err) {
                reject(err);
            }
            else {
                resolve(true);
            }
        }
    );
    })
}

isConnected().then(function(){
    // // queryDatabase();
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM