【问题标题】:How to add nano.uuid to the chain of promises that read from the DB?如何将 nano.uuid 添加到从数据库读取的承诺链中?
【发布时间】:2019-08-13 07:32:36
【问题描述】:

我正在将此代码与快速路由和 nano 一起使用:

router.get('/', function (request, response) {
  db.view('designdoc', 'bydate', { 'descending': true })
    .then(results => {
      // data manipulation of results, all blocking and fine
      return results;
    })
    .then(results => {
        nano.uuids(1)
          .then(uuids => {
            results.uuid = uiids['uuids'][0];
            resolve(); // return ?
          })
          .catch(error => {
          // ?
           });
      });
      return results;
    })
    .then(results => { response.json(results); }) // how to have results.uuid = something from the previous then ?
    .catch(error => { response.json(error); });

我想在结果中添加一个来自 nano.uuid 的 uuid,但我不知道如何在下一个 then 中操作承诺。

如何从nano.uuid获取数据,等待并添加到results

编辑

我正在切换到@narayansharma91 建议的异步方法,这段代码解决了我的问题:

    router.get('/', async function (request, response) {
      const results = await db.view('designdoc', 'bydate', { 'descending': true });
      var uuid = await nano.uuids(1);
      results.uuid = uuid.uuids[0];
      response.json(results);
    }

但我仍然想了解基于承诺的解决方案。

【问题讨论】:

  • 性能提示:没有理由依次运行await db.viewawait nano.uuids,可以使用Promise.all获得更好的响应时间:const [results, uuid] = await Promise.all([db.view('designdoc', 'bydate', { 'descending': true }), nano.uuids()])

标签: javascript node.js express promise pouchdb


【解决方案1】:
   function successcb(results){
    return new Promise(function(resolve, reject) {
    nano.uuids(1)
  .then(uuids => {
    results.uuid = uiids['uuids'][0];
    resolve(results);
  })
  .catch(error => {
    throw err;
   });
  });
}

router.get('/', function (request, response) {
  db.view('designdoc', 'bydate', { 'descending': true })
    .then(results => {

      return results;
    })
    .then(successcb)
    .then(results => { response.json(results); }) 
    .catch(error => { response.json(error); });

【讨论】:

  • @Mohit Sahu 有点不对劲。使用调试器,我可以看到结果获得了 uuid 属性,但最终在 final 中发送的响应没有它。
【解决方案2】:

您无需执行此长脚本即可获得结果。您可以使用async/await 执行相同的操作,如下所示。

 router.get('/', async function (request, response) {
     const result = await db.view('designdoc', 'bydate', { 'descending': true })
     console.log(result)//do what ever you want to with result
});

【讨论】:

    【解决方案3】:

    我没有对您的代码进行太大更改,因为您希望在 Promise 中找到解决方案。您不需要调用 resolve(),因为 .then() 会为您执行此操作。将 uuid 附加到结果后,只需返回结果即可。

    router.get('/', function (request, response) {
        db.view('designdoc', 'bydate', {
                'descending': true
            })
            .then(results => {
                // data manipulation of results, all blocking and fine
                return results;
            })
            .then(results => {
                results = nano.uuids(1)
                    .then(uuids => {
                        results.uuid = uiids['uuids'][0];
                        return results;
                    })
                    .catch(error => {
                        throw error;
                    });
                return results;
            }).then(results => {
                response.json(results);
            }) // how to have results.uuid = something from the previous then ?
            .catch(error => {
                response.json(error);
            });
    
    })
    

    【讨论】:

      【解决方案4】:
      router.get('/', function(request, response) {
                  let _results;
                  db.view('designdoc', 'bydate', {
                          'descending': true
                      })
                      .then(results => {
                          // data manipulation of results, all blocking and fine
                          // save results in tempopary variable
                          // since we want to use only one level of Promise chains
                          // we need to save results on controller scope level to allow access in further chains
                          _results = results;
                          // we don't need to return since every `then` by default return `Promise`
                      })
                      .then(() => {
                          // you can just return, since nano.uuids return Promise
                          // next then chain will be called once action completed
                          return nano.uuids();
                      })
                      .then(uuids => {
                          // use _results from controller level scope
                          // uuids will be injected as a result of return nano.uuids()
                          _results.uuid = uiids['uuids'][0];
                          response.json(_results);
                      })
                      .catch(error => {
                          response.json(error);
                      });
              }
      

      为了更好地理解Promise链,我总是建议阅读great article

      【讨论】:

        猜你喜欢
        • 2015-09-25
        • 1970-01-01
        • 2020-02-20
        • 1970-01-01
        • 1970-01-01
        • 2020-07-02
        • 1970-01-01
        • 2019-05-10
        • 2016-11-25
        相关资源
        最近更新 更多