Jump to content


Photo
Question

Getting Market Price (Async Function)

node.js node-steam-tradeoffer-manager

  • Please log in to reply
4 replies to this topic

#1 Meshi8

Meshi8

    Member

  • Member
  • PipPip
  • 13 posts

Posted 23 February 2019 - 03:53 AM

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, 02 March 2019 - 10:51 AM.


#2 Dr. McKay

Dr. McKay

    Developer

  • Administrator
  • 1,992 posts

Posted 23 February 2019 - 04:05 AM

ConvertToKeys2 is not an async function (nor does it return a Promise), so that's why await isn't awaiting anything.



#3 Meshi8

Meshi8

    Member

  • Member
  • PipPip
  • 13 posts

Posted 23 February 2019 - 04:49 AM

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, 23 February 2019 - 04:50 AM.


#4 Dr. McKay

Dr. McKay

    Developer

  • Administrator
  • 1,992 posts

Posted 25 February 2019 - 04:48 AM

Yes, that's returning a promise. You should put your code inside of the callback function and call resolve() once you're done.



#5 Meshi8

Meshi8

    Member

  • Member
  • PipPip
  • 13 posts

Posted 02 March 2019 - 02:09 AM

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/80...fa774045e31c27d

 

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, 02 March 2019 - 10:51 AM.






Also tagged with one or more of these keywords: Question, node.js, node-steam-tradeoffer-manager

3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users