Promise pending and rejected?

I’m looping through all the chains provided by covalent to get a crosschain balance with:

async function getMultiBalances() {

  const chains = await getAllChains();

  let totalBalances = [];

  let address = await getAddress();

  for (let chain of chains) {

    let chainId = chain.chain_id;

    let balances = await getMultichainBalancesArray(chainId, address);

    if (balances.length !== 0) {

      totalBalances = [...balances, ...totalBalances];

    }

    console.log(totalBalances);

  }

  return totalBalances;

}

console.log(getMultiBalances());

I get the balances array working as seen in the below screenshot, but when trying to return the final balances array it fails. I’m not quite sure what to do, any ideas?

can you try to look you your browser network tab to see if there are any errors there?
it looks like an error returned by a plugin

Only status 200 in network tab.

The thing is that the 10 items in an array is what i’m looking to return (user.js:85 in screenshot).

Works in console log, but the the function isn’t returning anything.

really not sure how to debug this either:

It says that chainId is not valid

yea but where should i try to debug this? which line

I have a feeling its the covalent call that returns the chainIDs and the last one is a bit random. But even then should just error out right and return the array.

In the network tab in your browser you should be able to see the exact request that is sent to the plugin and also the response with the error

There’s only status 200. No 404 errors

There should be a lot of requests in network tab, that looks like only js files

Yes its only js. But all the other ones aren’t really relevant to moralis right?

And still, only status 200 outside of that one redirect 302

I mean it looks like that is not the network tab with all the requests that are made to your Moralis server.

I think it’s just a javascript problem. If I return the code after the first iteration like this:

async function getMultiBalances() {
  let chains = await getAllChains();
  let address = await getAddress();
  let totalBalances = [];

  for (let chain of chains) {
    let chainId = chain.chain_id;
    let balances = await getMultichainBalancesArray(chainId, address);
    if (balances != undefined) {
      totalBalances = [...balances, ...totalBalances];
    }
    return totalBalances;
  }
  console.log(totalBalances);
  return totalBalances;
}

It works, but just not for all the chains as required. If i return totalBalances outside of the “for of” loop, it doesnt return anything. Not sure why.

The moralis errors also disappear with this.

I remember now something about variables in for, that could be the problem

It’s actually that the covalent getChains returns wrong chain IDs…

All these error out:

      chainsTwo[i].chain_id != 128 &&
      chainsTwo[i].chain_id != 8217 &&
      chainsTwo[i].chain_id != 2020 &&
      chainsTwo[i].chain_id != 336 &&
      chainsTwo[i].chain_id != 4689 &&
      chainsTwo[i].chain_id != 1131378225

This is the 2nd time i’ve spent an ungodly amount of time debugging, what in the end to me appears to be a moralis bug.