Can anyone suggest a better way to structure this use of Promises? I'm newish to Promises and am wondering if I'm missing something about how to construct this a chain of events.
Note: It IS my intention to NOT use the rej[ect] here. What you see guatanrees only the res[olve] returns. Meaning the code this returns to only needs one path with which to handle the returned value. Thereby the code returned to is simplier in it's flow.
It might help to know, if you don't recognize it, this is taken from a module I created. Think of it as a Dao.
module.exports = {
dbConnection: function () {
return { user: 'sa', password: 'mypassword', server: 'localhost', database: 'mydb' };
},
CanIConnectToTheDB: function () {
return new Promise(function (res, rej) {
var sql = require('mssql');
var myDao = require('./myDao');
var cn = new sql.ConnectionPool(myDao.dbConnection());
cn.connect().then(function () {
var req = new sql.Request(cn);
var qry = 'select serverproperty('productversion') as 'rs'';
req.query(qry)
.then(function (rs) {
qry = 'select isnull(object_id('SomeObjectIKnowExists'), -1)';
req.query(qry)
.then(function (rss) {
res(' CONNECTED// MASTER DB SUCCESS// MY DB SUCCESS');
})
.catch(function (err) {
res(' CONNECTED// MASTER DB SUCCESS// ISSUE QUERYING MY DB //' + err + '//');
});
})
.catch(function (er) {
res(' CONNECTED// COULD NOT QUERY MASTER DB //' + er + '//');
});
})
.catch(function () {
res(' CAN NOT CONNECT');
});
});
}
};
See Question&Answers more detail:os