最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Return Nested Promise - Stack Overflow

programmeradmin0浏览0评论

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 of sql.connect() and request.query()? That might just work. getColumn() will return a Promise though so you'll need another then 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
Add a ment  | 

1 Answer 1

Reset to default 8

First 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);
});
发布评论

评论列表(0)

  1. 暂无评论