Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

326
Vistas
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 :-)

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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)



about 4 years ago · Juan Pablo Isaza Denunciar

0

Because this function returns a value asynchronously, you need to return a data type that can handle asynchronous values.

In JS, that is called a promise. You can access their value by registering a callback using .then(), or in TypeScript and other modern JS environments, you can use the nice async/await syntax.

Here is how you would create and return a promise from your function.

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

// Using .then():
getUserInv('<my steamid>').then(inv => console.log(inv));

// Using async/await:
console.log(await getUserInv('<my steamid>'));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda