React中将async 同步调用接口变成异步调用

React中将async 同步调用接口变成异步调用,第1张

  useEffect(async () => {
    const fn1 = (() => {
      return new Promise(resolve => {
        const msg = '11';
        setTimeout(() => {
          console.log(msg, '11');
          resolve(msg)
        }, 2000);
      }).then(res => {
        return res
      })
    })()
    const fn2 = (() => {
      return new Promise(resolve => {
        const msg = '22';
        console.log(msg, '22');

        resolve(msg)
      }).then(res => {
        return res
      })
    })()
    const data1 = await fn1;
    const data2 = await fn2;
    console.log(data1, data2, 'data1');
  }, [])

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1320420.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-11
下一篇 2022-06-11

发表评论

登录后才能评论

评论列表(0条)

保存