如何将nano.uuid添加到从数据库读取的承诺链中?

时间:2019-03-22 10:21:19

标签: javascript node.js express promise pouchdb

我将此代码用于快速路由和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中操纵promise。

如何从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);
    }

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

4 个答案:

答案 0 :(得分:1)

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

async/await

答案 1 :(得分:0)

   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); });

答案 2 :(得分:0)

我对您的代码没有太大变化,因为您想要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);
        });

})

答案 3 :(得分:0)

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