admin管理员组文章数量:1355063
I am trying to return the result
parameter from the getColumn
function. When logging, it returns undefined.
The connection function connects to a SQL DB and the query returns a data set.
How can I pass the variable back up the promise chain?
getColumn = function(columnName, table) {
sql.connect(config.properties)
.then(result => {
let request = new sql.Request();
request.query("SELECT " + columnName + " FROM " + table)
.then(result => {
// want to return this result from the getColumn function
return result
}).catch(err => {
// Query error checks
})
}).catch(err => {
// Connection error checks
})
} //
console.log(getColumn('username', 'Login'))
I am trying to return the result
parameter from the getColumn
function. When logging, it returns undefined.
The connection function connects to a SQL DB and the query returns a data set.
How can I pass the variable back up the promise chain?
getColumn = function(columnName, table) {
sql.connect(config.properties)
.then(result => {
let request = new sql.Request();
request.query("SELECT " + columnName + " FROM " + table)
.then(result => {
// want to return this result from the getColumn function
return result
}).catch(err => {
// Query error checks
})
}).catch(err => {
// Connection error checks
})
} //
console.log(getColumn('username', 'Login'))
Share
Improve this question
asked Jan 3, 2018 at 18:38
angang
1,5814 gold badges28 silver badges59 bronze badges
3
-
1
Have you tried putting a
return
in front ofsql.connect()
andrequest.query()
? That might just work.getColumn()
will return a Promise though so you'll need anotherthen
there. – Halcyon Commented Jan 3, 2018 at 18:40 - then resolve that promise and get the result or you might need to use callbacks – Rahul Singh Commented Jan 3, 2018 at 18:42
-
1
Ok, then do something like
getColumn('username', 'Login').then((result) => console.log(result));
. – Halcyon Commented Jan 3, 2018 at 18:42
1 Answer
Reset to default 8First off, you can't return a value directly form getColumn()
. The insides of that function are asynchronous so the value won't be known until AFTER getColumn()
returns. You are currently getting undefined
from getColumn()
because it has no return value. The return
you do have is to an asynchronous .then()
handler, not for getColumn()
. There is no way to return the final value from getColumn()
. It's asynchronous. You have to either return a promise or use a callback. Since you're already using promises internal to the function, you should just return a promise.
You can return a promise from getColumn()
and use .then()
or await
with that promise to get the value.
To return a promise, you need to return the internal promises:
const getColumn = function(columnName, table) {
// return promise
return sql.connect(config.properties).then(result => {
let request = new sql.Request();
// chain this promise onto prior promise
return request.query("SELECT " + columnName + " FROM " + table);
});
} //
getColumn('username', 'Login').then(val => {
console.log(val);
}).catch(err => {
console.log(err);
});
本文标签: javascriptHow to Return Nested PromiseStack Overflow
版权声明:本文标题:javascript - How to Return Nested Promise - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743990887a2572114.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论