What Comes Around Posted April 21, 2020 Report Posted April 21, 2020 Good day, So I am kind of new to js and this may be a noob question, if it is I am very sorry for asking. But here is what I am having issues with: const getInventory = () => { manager.loadInventory(730, 2, true, (err, inventory) => { if (inventory) { return inventory; } if (err) { console.log(err); } }) } So I export this function, but when I try console logging it, it doesn't return anything, just undefined. But when I put inventory into a callback, I get it. Is there something I am missing here? Thanks! Quote
vrtgn Posted April 21, 2020 Report Posted April 21, 2020 (edited) I also made this mistake as a beginner. You have a misunderstanding of asynchronous code in javascript. Fetching the inventory takes some time, and so by returning inventory, the variable is being returned before the program can even fetch your inventory. You will have to create your own callback and that works by taking in a function as a callback parameter, and then executing that function once the inventory has been fetched. const getInventory = (callback) => { manager.loadInventory(730, 2, true, (err, inventory) => { if (err) { callback(err); } else { callback(null, inventory); } }) } I personally prefer to check for errors first, but whatever you prefer you can change it to. When we want to fetch the inventory we use it like so: getInventory((err, inventory) => { if (err) { console.log('error getting the inventory :('); } else { // code to do whatever you want with inventory here } }) I also saw that you are using the loadInventory method, it would be much better if you used the getInventoryContents method as it says this in the docs: Quote Don't rely on the older endpoint and the deprecated loadInventory method as it will likely be removed in the future. EDIT: Callbacks are getting ugly nowadays imo so you can also use a promise if you prefer: const getInventory = () => { return new Promise((resolve, reject) => { manager.loadInventory(730, 2, true, (err, inventory) => { if (err) { reject(err); } else { resolve(inventory); } }); }); } getInventory() .then(inventory => { // do whatever here }) .catch(err => console.log('oops')); // OR // this must be inside a async function try { const inventory = await getInventory(); // do whatever here } catch (err) { console.error(err) } Edited April 22, 2020 by vrtgn Added try catch as Doc suggested. Dr. McKay and What Comes Around 1 1 Quote
What Comes Around Posted April 21, 2020 Author Report Posted April 21, 2020 6 minutes ago, vrtgn said: I also made this mistake as a beginner. You have a misunderstanding of asynchronous code in javascript. Fetching the inventory takes some time, and so by returning inventory, the variable is being returned before the program can even fetch your inventory. You will have to create your own callback and that works by taking in a function as a callback parameter, and then executing that function once the inventory has been fetched. const getInventory = (callback) => { manager.loadInventory(730, 2, true, (err, inventory) => { if (err) { callback(err); } else { callback(null, inventory); } }) } I personally prefer to check for errors first, but whatever you prefer you can change it to. When we want to fetch the inventory we use it like so: getInventory((err, inventory) => { if (err) { console.log('error getting the inventory :('); } else { // code to do whatever you want with inventory here } }) I also saw that you are using the loadInventory method, it would be much better if you used the getInventoryContents method as it says this in the docs: This makes perfect sense! I tried using a callback and it worked, but I didn't like the mess I created so I made the code in my original question. Your solution is clean and makes sense. Also I will use getInventoryContents. Thanks a bunch! 9 minutes ago, vrtgn said: I also made this mistake as a beginner. You have a misunderstanding of asynchronous code in javascript. Fetching the inventory takes some time, and so by returning inventory, the variable is being returned before the program can even fetch your inventory. You will have to create your own callback and that works by taking in a function as a callback parameter, and then executing that function once the inventory has been fetched. const getInventory = (callback) => { manager.loadInventory(730, 2, true, (err, inventory) => { if (err) { callback(err); } else { callback(null, inventory); } }) } I personally prefer to check for errors first, but whatever you prefer you can change it to. When we want to fetch the inventory we use it like so: getInventory((err, inventory) => { if (err) { console.log('error getting the inventory :('); } else { // code to do whatever you want with inventory here } }) I also saw that you are using the loadInventory method, it would be much better if you used the getInventoryContents method as it says this in the docs: Oh but one question. getInventoryContents has a steamid param, so I'd assume you can fetch other users inventories, but there is a getUserInventoryContents function for that. What's the difference? vrtgn 1 Quote
vrtgn Posted April 21, 2020 Report Posted April 21, 2020 11 minutes ago, What Comes Around said: Oh but one question. getInventoryContents has a steamid param, so I'd assume you can fetch other users inventories, but there is a getUserInventoryContents function for that. What's the difference? You may have made a mistake, getInventoryContents does not have a steam ID param and is used exclusively for fetching the account's inventory, where as getUserInventoryContents like you correctly said is used to fetch other users inventories. Quote
What Comes Around Posted April 21, 2020 Author Report Posted April 21, 2020 2 minutes ago, vrtgn said: You may have made a mistake, getInventoryContents does not have a steam ID param and is used exclusively for fetching the account's inventory, where as getUserInventoryContents like you correctly said is used to fetch other users inventories. Yes I was mistaken, my bad. Thanks again! Your code is very clean, would have taken me a while to get all the mess I had perfected. vrtgn 1 Quote
Dr. McKay Posted April 22, 2020 Report Posted April 22, 2020 Thanks so much for the help @vrtgn. It may be worth noting that if you're using async, you don't need to use .catch((err) => . . .). You can use try/catch: try { const inventory = await getInventory(); // do whatever } catch (err) { console.error(err); } vrtgn and What Comes Around 2 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.