然后在内部调用异步函数 [英] calling a async function inside then

查看:44
本文介绍了然后在内部调用异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段如下所示的代码:

I have a piece of code that looks like this:

func().then(function (result){
   var a = func1(result);
   func2(a).then(function(result1){
    ////
   }
}

如你所见,func 返回一个 promise,然后我们调用另一个 func1,它也返回一个 promise.是否可以将 func2 返回的承诺与 then 的承诺链接起来,并以某种方式在第二个 then 中使用嵌套函数.

As you can see func returns a promise, and in then part we call another func1 which also returns a promise. Is it possible to chain the promise returned from func2 with the promise of then, and somehow get ride of the nested functions in the second then.

推荐答案

then() 函数内的返回值用作承诺值本身.所以你可以轻松地返回新的 promise 并在此处继续链接:

The return value inside a then() function is used as a promise value itself. So you can easily return the new promise and keep on chaining here:

func()
  .then(function (result){
    var a = func1(result);
    return func2(a);
  })
  .then(function(result1){
    ////
  })

参见 2.2.72.3.2.

这篇关于然后在内部调用异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆