【问题标题】:How to call a function with a promise如何使用 Promise 调用函数
【发布时间】:2018-08-01 03:43:21
【问题描述】:

我对 JS 相当陌生,我正在尝试理解 npm 包中的文档。文档是:

client.projects.get(); // Promise

我已经阅读了一些关于 Promises 的文档,但我仍然不确定如何调用它并让它返回我所期望的。

包在这里供参考:https://github.com/markmssd/bitbucket-server-nodejs

【问题讨论】:

  • 你显然没有读过关于 Promise then.
  • 请添加您尝试过的内容。你有一个Promise,你已经从.get()回来了。你试图用它做什么?您阅读的文档说与它有什么关系?什么没用?你觉得哪里出了问题。你现在的问题太宽泛了。

标签: javascript node.js promise


【解决方案1】:

Promise 是代码的异步执行。

您可以使用 Promise 上的 .then 方法获取从该异步代码返回的值。您必须传递处理返回值的回调函数。

client.projects.get().then(function(foo) {
// this foo is returned from client.projects.get() async operation
})

如果异步操作引发了一些异常,您可以在 Promise 上使用 .catch 捕获那些异常。

client.projects.get().then(function(foo) {
    // this foo is returned from client.projects.get() async operation
}).catch(function(err) {
   // something went wrong while executing client.projects.get()
})

【讨论】:

  • 谢谢 - 这是有道理的,即使在阅读了这方面的文档后,我也只是遇到了语法问题。
  • 语法有什么问题?
【解决方案2】:

client.projects.get(); 将返回一个 promise 而可能不是“您所期望的”。

你应该这样称呼它:

client.projects.get().then((result) => {
    // do with `result` your logic
    console.log(result);
});

然后在作为参数传递给then 函数的回调内部接收响应提供的result,并根据您的逻辑使用它。

【讨论】:

    【解决方案3】:

    玩弄:

    client.projects.get().then(result => console.log(result))
    

    在使用 Promise 时,您会注意到,您需要指定在其结果准备好后如何处理它。

    您只需返回结果的替代方法是:

    client.projects.get().then(res => res)
    

    如果出现错误,您还需要添加一个 catch:

    client.projects.get().then(res => res).catch(err => console.error(err))
    

    如果出现故障或某种故障,这将注销错误。

    【讨论】:

      【解决方案4】:

      > p.then(onFulfilled[, onRejected]);

      onFulfilled - 如果 Promise 实现,则调用一个函数。

      onRejected(可选) - 如果 Promise 被拒绝,则调用一个函数。

      p.then(function(value) {
         // fulfillment
       }, function(reason) {
        // rejection
       });
      

      > 尝试 -> 捕捉 -> 最终

      p.then(function(data) { console.log("Play with your data.") })
         .catch(function(error) { console.log(error); })
         .finally(function() { console.log("Something need to do, no matters fail or sucess") });
      
      1. finally() 方法非常有用,如果您想在承诺完成后进行一些处理或清理,而不管其结果如何。
      2. finally() 方法与调用 .then(onFinally, onFinally) 非常相似

      了解更多details


      因此,您可以编写如下代码:

       client.projects.get().then(function(value) {
          // fulfillment
        }, function(reason) {
          // rejection
        });
      

      或者

       client.projects.get()
         .then(function(data) { console.log("Play with your data.") })
         .catch(function(error) { console.log(error); })
         .finally(function() { console.log("Finally do something.") });
      

      【讨论】:

        【解决方案5】:

        Promise 对象表示可能尚不可用但将在未来某个时间解决的值。它允许您以更同步的方式编写异步代码。一旦 promise 解决,您就可以获得结果,或者在 promise reject(failure) 的情况下捕获错误。在您的情况下,您必须像这样调用函数:

          client.projects.get().then(function(result){
               console.log(result);
            }).catch(function(err) {
                // handle error
                console.log("something went wrong",err);
             });
        

        另外,您也可以将 promise 存储到函数调用返回的变量中并获取结果,如下所示:

        var promise = client.projects.get();
        
        promise.then(function(result){
           console.log(result);
        }).catch(function(err) {
            // handle error
            console.log("something went wrong",err);
         });
        

        将 Promise 赋值给一个变量可能不是一个好的选择,但是当有多个函数返回一个 Promise 并且我们希望在所有 Promise 都解决后执行一些代码时,它非常有用。像这样的:

        var p1 = asyncFunction1();
        var p2 = asyncFunction2();
        Promise.all([p1,p2]).then(function(results){
         // do something with results
        });
        

        You can also check this nice blog on promises

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-04-27
          • 2016-05-26
          • 1970-01-01
          • 2018-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多