一秒内访问先前承诺的结果 [英] Access result of previous promise inside a second

查看:59
本文介绍了一秒内访问先前承诺的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 return pattern 来防止我做出丑陋的级联承诺.这是一个例子,我一个接一个地调用两个函数 myfunction1myfunction2

I'm using the return pattern to prevent me from making promise ugly cascade. Here is an exemple, I'm calling two function one after the other myfunction1 and myfunction2

myfunction1().then((value1) => {
  return myfunction2()
}).then((value2) => {
  console.log(value1)
}).catch((err) => {
  console.error(err)
})

如何在 seconde 函数的 then 中访问 value1 ?

How can I access value1 inside the then of the seconde function ?

推荐答案

你必须通过你的链来传递它.这就是为什么我开始使用 async/await:

You must pass it through your chain. That's why I started using async/await:

try {
    var value1 = await myfunction1();
    var value2 = await myFunction2();
    console.log(value1)
} catch (err) {
    console.error(err)
}

这篇关于一秒内访问先前承诺的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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