Is it bad practice to resolve a promise in the 'then' of another promise chain?
Example:
function getConnection() {
return new Promise(function(resolve, reject) {
connect(function(err, conn) {
if (err) {
reject(err);
return;
}
resolve(conn);
});
});
}
function query(sql) {
return new Promise(function(resolve, reject) {
getConnection()
.then(function(conn) {
conn.query(sql, function (err, results){
resolve(results)
});
})
.catch(function(err) {
reject(err)
})
});
}
The idea is that the 'query' Promise would be frequently used in the app. So instead of typing out the promise chain steps of connecting then something else then querying. I have a quick Promise that I drop in when needed.
Keep in mind that this example is simpler so there would be a few more chain steps that are abstracted away.
So my question is am I falling into some Promise anti pattern? I looked around and I didn't see my exact example. Are there any performance issues? Are there scenarios where my error is swallowed or my Promise is never fulfilled? Or am I simply over thinking the problem and creating a convoluted solution?
Regards