如何从 {"_U": 0, "_V": 0, "_W": null, "_X": null} 中获取函数返回响应

发布于 2025-01-10 13:17:22 字数 782 浏览 0 评论 0 原文

我试图在循环中调用一个函数,并且函数执行一些 https 请求,响应返回到函数并返回 :

{"_U": 0, "_V": 0, "_W": null, "_X": null}

我想将确切的值返回到循环中调用函数的那个​​键。

这是我的代码

 res.forEach(async (elem, index) => {
      await arr.push({
        id: index,
        name: '',
        color: '#2F14A8',
        chain: elem.chain,
        address: elem.walletAddress,
        balance: getWalletBalance(elem.walletAddress), //<---- **function call**

        img: images[index % images.length],
      });
    });

这是函数

const getWalletBalance =async address => {

let tokensArr=[] // some arry

    const res = await dispatch(getBalanc(address, tokensArr));
    return res;
    
    }

I'm trying to call a function in loop and function doing some https request the response is returning back to function and it returns :

{"_U": 0, "_V": 0, "_W": null, "_X": null}

I want to return the exact value to that key where the function is called in the loop.

Here is my code :

 res.forEach(async (elem, index) => {
      await arr.push({
        id: index,
        name: '',
        color: '#2F14A8',
        chain: elem.chain,
        address: elem.walletAddress,
        balance: getWalletBalance(elem.walletAddress), //<---- **function call**

        img: images[index % images.length],
      });
    });

Here is the function :

const getWalletBalance =async address => {

let tokensArr=[] // some arry

    const res = await dispatch(getBalanc(address, tokensArr));
    return res;
    
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅的被狗咬 2025-01-17 13:17:22

getWallertBalance 函数中 res 的响应是什么?
我认为如果你在 forEach 中的 getWalletBalance(elem.walletAddress) 之前放置 wait 就可以解决问题!

What is the response for res in getWallertBalance function?
I think if u put await before getWalletBalance(elem.walletAddress) in forEach solves the problem!

若言繁花未落 2025-01-17 13:17:22

这些对 getWalletBalance 的单独调用似乎并不相互依赖。最好的方法是使用 map 生成一个 Promise 集合,同时解析它们......

const getWalletBalance = async address => {
  const res = await dispatch(getBalanc(address, tokensArr));
  const promises = res.map(element => getWalletBalance(elem.walletAddress));
  const balances = await Promise.all(promises);
  return res.map((element, index) => {
    return {
      id: index,
      name: '',
      color: '#2F14A8',
      chain: element.chain,
      address: element.walletAddress,
      balance: balances[i],
      img: images[index % images.length],
    };
  })
}

Those individual calls to getWalletBalance don't appear to depend on each other. The best way to do this is to produce a collection of promises with map, the resolve them concurrently...

const getWalletBalance = async address => {
  const res = await dispatch(getBalanc(address, tokensArr));
  const promises = res.map(element => getWalletBalance(elem.walletAddress));
  const balances = await Promise.all(promises);
  return res.map((element, index) => {
    return {
      id: index,
      name: '',
      color: '#2F14A8',
      chain: element.chain,
      address: element.walletAddress,
      balance: balances[i],
      img: images[index % images.length],
    };
  })
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文