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

javascript - How to pass variable through promise chain using finally - Stack Overflow

programmeradmin1浏览0评论

I am trying to build a log for an Express API, however am having issues getting the data to log out.

I can log the original req and res objects in the finally block, but am not sure how I would access the SQL response.

const sql = require("mssql")
const config = require("../config")

router.get("/express-route", (req, res) => {
  sql.connect(config.properties).then(pool => {
    return pool.request()
      .input('username', sql.NVarChar(32), req.params.username)
      .execute('do_something_with_username')
      .then(response => res.send(response) // pass this response
      .catch(err => res.send(err))
      .finally(() => {
        console.log('response', response)  // to here
        sql.close()
      })
  })
}

How would I take the response from the first then block and pass it to the finally block to be used in another function?

I am trying to build a log for an Express API, however am having issues getting the data to log out.

I can log the original req and res objects in the finally block, but am not sure how I would access the SQL response.

const sql = require("mssql")
const config = require("../config")

router.get("/express-route", (req, res) => {
  sql.connect(config.properties).then(pool => {
    return pool.request()
      .input('username', sql.NVarChar(32), req.params.username)
      .execute('do_something_with_username')
      .then(response => res.send(response) // pass this response
      .catch(err => res.send(err))
      .finally(() => {
        console.log('response', response)  // to here
        sql.close()
      })
  })
}

How would I take the response from the first then block and pass it to the finally block to be used in another function?

Share Improve this question asked Jul 10, 2019 at 20:35 angang 1,5814 gold badges28 silver badges59 bronze badges 6
  • You can't - if there was an error in request or input or execute, the finally callback runs but there never was a response value. So what are you actually trying to achieve? – Bergi Commented Jul 10, 2019 at 20:53
  • @Bergi Trying to capture data like the request body, request headers, response body, etc. to write to a log in SQL. – ang Commented Jul 10, 2019 at 20:54
  • 1 You should write that log when you are sending the response, not after you handled an error. You certainly will need to capture different data to your log in case there was an error. finally is not the right place to do that – Bergi Commented Jul 10, 2019 at 20:55
  • Is there another verb I can use other than then to help break up the visual flow during the scope of the log? – ang Commented Jul 10, 2019 at 20:58
  • 1 "then" is generally classed as an adverb, not verb :-) – Roamer-1888 Commented Jul 10, 2019 at 21:40
 |  Show 1 more ment

2 Answers 2

Reset to default 7

A finally callback will not receive any argument, since there's no reliable means of determining if the promise was fulfilled or rejected. This use case is for precisely when you do not care about the rejection reason, or the fulfillment value, and so there's no need to provide it. (mdn)

Instead, simply use .then:

const sql = require("mssql")
const config = require("../config")

router.get("/express-route", (req, res) => {
  sql.connect(config.properties).then(pool => {
    return pool.request()
      .input('username', sql.NVarChar(32), req.params.username)
      .execute('do_something_with_username')
      .then(response => {res.send(response); return response;}) // pass this response
      .catch(err => res.send(err))
      .then(response => {
        console.log('response', response)  // to here
        sql.close()
      })
  })
}

You can, in fact, simplify things by writing your code within an async function

const sql = require("mssql")
const config = require("../config")

router.get("/express-route", (req, res) => {
  sql.connect(config.properties).then(async pool => {
    try {
        const response = await pool.request()
                         .input('username', sql.NVarChar(32), req.params.username)
                         .execute('do_something_with_username');

        // do another request
        const otherResponse = await pool.request() ... ;

        res.send(response);
    } catch (err) {
        res.send(err);
    } finally {
        sql.close();
    }
  })
}

This lets you write code in a more linear manner.

发布评论

评论列表(0)

  1. 暂无评论