Jump to content
McKay Development

Getting Market Price (Async Function)


Meshi8
 Share

Recommended Posts

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 by Meshi8
Link to comment
Share on other sites

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 by Meshi8
Link to comment
Share on other sites

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 by Meshi8
Link to comment
Share on other sites

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...
 Share

×
×
  • Create New...