Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i return something from a function like this?

Im quite new to typescript and i have a fuction that will fetch user’s steam inventory and store it in "inventory" parameter, but when i want to return it it will return undefined:

function getUserInv(steamid) {
    manager.getUserInventoryContents(steamid, 440, 2, true, function(err, inventory) {
        if(err) {
            console.log(err);
        } else {
            return inventory
        };
    });
};

console.log(getUserInv('<my steamid>')) //returns undefined

How can i make it so that when i call it like above, it returns the inventory? Any help is appriciated 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can wrap it into a Promise like this

function getUserInv(steamid) {
    return new Promise((resolve, reject) => {
    manager.getUserInventoryContents(steamid, 440, 2, true, function(err, inventory) {
        if(err) {
            reject(err);
        } else {
            resolve(inventory)
        };
    });
}
};

getUserInv('<my steamid>').then(console.log)



Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading