2

I need to read a promise and do things with the data in the promise object but I am not sure how to work with it.

Below is the function I am using to initially pull the data:

export async function getBalance(coin, walletId) {

    const req_url = process.env.NEXT_PUBLIC_FIREBLOCKS_SERVER + "/get_balance";

    return fetch(req_url, {
        method: 'POST',
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
            'Access-Control-Allow-Origin': '*'
        },
        body: JSON.stringify({
            id: walletId,
            asset: coin,
        })
    }).then(response => response.json())
        .then(data => data.balance)
        .then((balance)  => {

            console.log('balance for ' + walletId + ': ' + balance)
            return balance

        }).catch(error => {
            console.log(error)
        })
}

Then I am placing the data into a Map, like below:

export function getAllBalances(coin, wallets) {
    let balances = new Map()

    for(const element of wallets) {

        balances.set(element.id, getBalance(coin, element.id));
    }

    return balances;
}

Everything works out smoothly and it produces the Map below: enter image description here

I need the values in the promise (i.e. 0, 0.0001, etc.) how do I extract them?


Updated with the Fix

Adding async and pulling out the values a little more in the getAllBalances function helped, see the revised function below:

export async function getAllBalances(coin, wallets) {
    let balances = new Map()

    console.log("getting all balances")
    console.log(wallets)

    for(const element of wallets) {

        console.log(element.id)
        await getBalance(coin, element.id).then(
            (balance) => {
                console.log(balance)
                balances.set(element.id, balance)
            }
        )
    }

    console.log(balances)

    return balances;
}
Mabel Oza
  • 557
  • 8
  • 22
  • 3
    `getBalance` returns a Promise - so `getBalance(coin, element.id)` is a Promise, not the `value` you think it is ... it's in `getAllBalances` that you should be using `async`/`await` ... no need for `getBalance` to be `async` since you never `await` ... you just return the promise returned by fetch – Jaromanda X Jul 05 '22 at 02:54
  • 1
    You can invoke a `thenable` by adding a `then` method on the promise in `balances.set`. You can also make the `set` method `async` and wait for the promise to resolve there (which I think is what you want) or you collect a list of promises of `set` and then invoke `Promise.all` on them and return the resulting array. – Abrar Hossain Jul 05 '22 at 02:56
  • 2
    as an aside ... what is `'Access-Control-Allow-Origin': '*'` doing in the request headers? Get rid of it, it's purely a response header and will only potentially break a request – Jaromanda X Jul 05 '22 at 02:58
  • Thank you! The async helped! I added my revised code above – Mabel Oza Jul 05 '22 at 03:41
  • 2
    add `async` before `function getAllBalances` and add `await` before `getBalance`. – Mulan Jul 05 '22 at 03:43

0 Answers0