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)
}
async/awaitwith.then(). Uselet response = await axios(...)