Meshi8 Posted February 23, 2019 Report Posted February 23, 2019 (edited) Hey, so Im trying to get the market price of an item I receive on a trade. It works, but for some reason the script won't wait for my async function to finish and returns an empty result First I have this function which is triggered when I receive an offer for their items async function ProccessTradeOffer(offer){ let TheirItems = offer.itemsToReceive; const Deposited_Items = await ConvertToKeys2(TheirItems); console.log(Deposited_Items); // this will print null and won't wait for the async to finish } This is the other function im calling async function ConvertToKeys2(Items){ let ValueInKeys = []; // array of objects containing info about the items return new Promise(resolve => { setTimeout(() => { if(Market_Value > -1){ obj = { name: item_name, value: Market_Value } ValueInKeys.push(obj); } }, 2000); }); } } return ValueInKeys; } The problem is that in the main async function it'll not wait for my ConvertToKeys2 function to calculate & add it to array and simply print an empty array.It's an async function so I don't understand why thisis happening.. Thx for your help Edited March 2, 2019 by Meshi8 Quote
Dr. McKay Posted February 23, 2019 Report Posted February 23, 2019 ConvertToKeys2 is not an async function (nor does it return a Promise), so that's why await isn't awaiting anything. Quote
Meshi8 Posted February 23, 2019 Author Report Posted February 23, 2019 (edited) ConvertToKeys2 is not an async function (nor does it return a Promise), so that's why await isn't awaiting anything.What about this? return new Promise(resolve => {}); Doesn't that mean it returns a promise? Sry if you facepalm reading this, I'm still trying to wrap my head around async functions Edited February 23, 2019 by Meshi8 Quote
Dr. McKay Posted February 25, 2019 Report Posted February 25, 2019 Yes, that's returning a promise. You should put your code inside of the callback function and call resolve() once you're done. Quote
Meshi8 Posted March 2, 2019 Author Report Posted March 2, 2019 (edited) Yes, that's returning a promise. You should put your code inside of the callback function and call resolve() once you're done. async function abc(){ // test function let Items = ["AK-47 | Elite Build (Minimal Wear)","AK-47 | Redline (Field-Tested)","AK-47 | Frontside Misty (Field-Tested)"]; let prices = await Market_Price(Items); console.log(prices); // this will print an empty array } This is what happens: https://gyazo.com/80c78f9ea22da25b0fa774045e31c27d It will not wait for the function to finish.They're both async, it returns a promise but im not sure about the resolve() posittion Edit: Figuered it out. Just wrapped the entire function with return promise like you suggested, thx for the help Edited March 2, 2019 by Meshi8 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.