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

453
Vistas
Javascript async/await return value is undefined, why?

I have a function that posts data via an API. I want to return the response...I am following tutorials online to try to understand async/await, and the way I have it laid out now seems to be correct to me.

I can print inside the postAsset.then statement, but the return value is always undefined. That's what I thought await fixed, unsure why it is still undefined in this case?

const postAsset = async (assetToPost) => {
    let token = await retrieveToken()

    if (token !== undefined && token !== 'Error') {
        const config = {
            headers: { Authorization: `Bearer ${token}` }
        };

        let urlAssets = `${API_ROUTE}${ASSETS.ASSETS_API_URL}`

        axios
            .post(urlAssets, assetToPost, config)
            .then(function (response) {
                let assetId = response.data
                return assetId
            })
            .catch(function (error) {
                console.log('POSTING ASSET ERROR: ', error.message);
                return -1
            })
    }
}

const postData = async () => {
        // post asset, get id response.
        // then post beacons in loop, get id response for each
        // then post a beaconId as part of assetId

        let assetId = await postAsset(asset)
        console.log(assetId)
}
about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

Because you are using .then and not await - which would work if you'd return the promise that you get, but you don't.

Solution 1:

Return the promise you get from axios.post (add return in front of it):

return axios
    .post(...)
    .then(...)

Solution 2 (better):

Don't mix .then with async/await, instead only use async/await:

try {
  const response = await axios.post(urlAssets, assetToPost, config)
  return response.assetId
} catch (error) {
  console.log('POSTING ASSET ERROR:', error)
  return -1
}

(By the way, I changed error.message to just error so you won't lose the important stack information!)

about 4 years ago · Santiago Gelvez Denunciar

0

the function postAsset should return a promise, and within the promise should execute the call axios.post

about 4 years ago · Santiago Gelvez Denunciar

0

You're returning your assetId from the axios call but not returning anything from the postAsset method.
Just add the return keyword before you axios call. It will work.

    return axios
        .post(urlAssets, assetToPost, config)
        .then(function (response) {
            let assetId = response.data
            return assetId
        })
        .catch(function (error) {
            console.log('POSTING ASSET ERROR: ', error.message);
            return -1
        })
about 4 years ago · Santiago Gelvez 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