nickmura aka bobby Posted September 4, 2023 Report Posted September 4, 2023 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. 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? Quote
Dr. McKay Posted September 5, 2023 Report Posted September 5, 2023 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(); } }); }); }); } Quote
nickmura aka bobby Posted September 5, 2023 Author Report Posted September 5, 2023 (edited) Maybe my code block was misleading. The problem is with the getKeyBalance(). I'm not very familiar with promises, I'll try implementing in the balance function. thank you Edited September 5, 2023 by nickmura aka bobby thanks Quote
Recommended Posts
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.