Jump to content
McKay Development

Recommended Posts

Posted

I'm trying to make a function called getKeyBalance, which should return the the amount of a specific item a user has in their inventory.

 

export const getKeyBalance = (manager:TradeOfferManager) => { // returns bots key balance
    const TF2_KEY_CLASSID = '101785959'
    let keys = 0
    manager.getInventoryContents(TF2_APPID, INV_CONTEXT_ID, true, (err, inv) => {
        if (!err) {
            for (let i = 0; i != inv.length; i++) {
                let item = inv[i]
                if (String(item.classid) == TF2_KEY_CLASSID) keys++;
            } console.log('manager.getInventoryContents() keys value:', keys)
            return keys
        } else {
            console.log('getUserKeyBalance error:', err)
            return 0
        }
    });
}

I'm calling it in another function, called `sendBuyOffer()`,

 

export const sendBuyOffer = async (manager:TradeOfferManager, withdraw:any) => {
    const keys = getKeyBalance(manager);
    console.log('getKeyBalance returns', keys)
    if (Number(keys) <= withdraw.keys) {
        createTradeOffer(manager, withdraw)
    }
}

const createTradeOffer = (manager:TradeOfferManager, withdraw:any) => {
    const offer = manager.createOffer(withdraw.steamid)
    manager.getInventoryContents(TF2_APPID, INV_CONTEXT_ID, true, (err, inv) => {
        if (!err) {
            let keys = 0
            for (let i = 0; i != inv.length; i++) {
                let item = inv[i]
                if (String(item.classid) == '101785959') {
                    offer.addMyItem(item);
                    keys++;
                } if (keys == withdraw.keys) break
            }
            offer.send((err, status) => {
                if (err) {
                    console.log(err);
                    return false;
                } else {
                    console.log(`Sent offer. Status: ${status}.`);
                    return true
                }
            });
        } return false
    });
}



However, the condition below the `getKeyBalance()` call never runs, because the function doesn't return a value in time. 

image.png.4de2edd9021bc4d593153505cbce0f53.png

As you can see, the returned value for `getKeyBalance()` is undefined, because the manager.getInventoryContents() doesn't return a value in time.

Why isn't this method asynchronous? It would fix this problem entirely. 
How else can I fix this, without running all the logic at once?



 

Posted

It is async. You can't use return inside a callback function and expect the outer function to get the value. To accomplish what you want, you could wrap it in a promise:

export const sendBuyOffer = async (manager:TradeOfferManager, withdraw:any) => {
    const keys = getKeyBalance(manager);
    console.log('getKeyBalance returns', keys)
    if (Number(keys) <= withdraw.keys) {
        // Note the addition of `await`
        await createTradeOffer(manager, withdraw)
      	// At this point, `resolve()` will have been called in the createTradeOffer function
      	// Or, if sending the offer failed, an Error would have been thrown, which could be caught using try/catch
    }
}

const createTradeOffer = (manager:TradeOfferManager, withdraw:any) => {
  	return new Promise((resolve, reject) => {
        const offer = manager.createOffer(withdraw.steamid)
        manager.getInventoryContents(TF2_APPID, INV_CONTEXT_ID, true, (err, inv) => {
          	if (err) {
              	return reject(err);
            }
          	
            let keys = 0
            for (let i = 0; i != inv.length; i++) {
              let item = inv[i]
              if (String(item.classid) == '101785959') {
                offer.addMyItem(item);
                keys++;
              } if (keys == withdraw.keys) break
            }
            offer.send((err, status) => {
              if (err) {
                console.log(err);
                reject(err);
              } else {
                console.log(`Sent offer. Status: ${status}.`);
                resolve();
              }
            });
        });
    });
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...