简体   繁体   English

如何在返回新诺言的函数中使用另一个诺言?

[英]How to use another promise in a function which returns a new promise?

I started learning Promises in JS and I am trying to replace my existing callback logic using promises. 我开始学习JS中的Promises,并尝试使用Promise替换现有的回调逻辑。 I wrote a function which returns a new promise, and also uses a promise of a database instance to retrieve the data. 我编写了一个函数,该函数返回一个新的Promise,并且还使用数据库实例的Promise来检索数据。 However, i am not sure if i am doing it right. 但是,我不确定我是否做对了。 Here is the code snippet, 这是代码片段,

usersService.js usersService.js

var getUsers = function(queryObject) {
    return new Promise(function(resolve, reject) {
        dbConnection.find(queryObject)
          .then(function(result) {
              if (result.length > 0) {
                resolve(result)
              } else {
                resolve(errorMessage.invalidUser())
              }).catch(function(err) {
              reject(err)
            });
          })
    };

usersRouter.js usersRouter.js

router.get('/users', function (req,res,next) {
     var queryObject = { "userId":req.query.userId };
     userService.getUsers(queryObject)
          .then(function (data) { //some logic })
          .catch(function (err) { //some logic });
});
  1. Can i use resolve conditionally ? 我可以有条件地使用resolve吗?
  2. If the answer is No , what is the right approach ? 如果答案是否定的 ,那么正确的方法是什么?
  3. Also, am i using the promise in a right manner, in the router? 另外,我是否在路由器中以正确的方式使用了Promise?

Thanks in advance! 提前致谢!

Since dbConnection.find returns a promise, you can return it directly and choose what will be pass when you will resolve it. 由于dbConnection.find返回了一个Promise,因此您可以直接将其返回,并选择解决它时将传递的内容。 No need to wrap it inside an other promise. 无需将其包装在其他承诺中。

var getUsers = function (queryObject) {
    return dbConnection.find(queryObject).then(function (result) {
       if (result.length > 0) {
          return result
       } else {
          return errorMessage.invalidUser()
       }
    })
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对一个调用另一个返回promise的函数进行单元测试? - How to unit test a function which calls another that returns a promise? 未捕获(在承诺中):如何等待在循环内返回承诺的函数? - Uncaught (in promise):How to await a function which returns a promise inside a loop? 如何创建一个返回现有承诺而不是新承诺的函数? - How to create a function that returns an existing promise instead of new promise? 如何测试(使用 Jest)返回包含另一个 promise 的 promise 的 function - How to test (using Jest) a function that returns a promise that contain another promise 将一个返回foo的promise映射到另一个返回bar的promise? - Mapping a promise which returns foo, to another promise which returns bar? Function 返回一个回调或 promise - Function which returns a callback or a promise 在返回新 Promise 的 function 中使用 Promise - Use Promise inside a function that return new Promise 如何包装返回承诺的延迟函数,以便外部函数返回实际值而不是承诺 - How to wrap deferred function which returns promise so outer function returns real value not a promise 如何定义将Promise返回到Express Route功能的功能? - How to define a function which returns Promise to a Express Route function? 如何绑定表示路由返回Promise的某些功能? - How to bind to express route some function which returns Promise?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM