Answer the question
In order to leave comments, you need to log in
await in axios not working?
Hi friends!
There is f-I probSetter which establishes to each order any probability. This function sends a get request to the server, and should return a response. But for some reason it returns a promise.
async function probSetter(order_id) {
return await axios.get(`http://site.local/calc-probs?order_id=${order_id}`)
}
let arr = [1, 2]
arr.forEach(element => {
let prob = probSetter(element)
console.log(`${element} = ${prob}`)
});
1 = [object Promise]
2 = [object Promise]
Answer the question
In order to leave comments, you need to log in
async function probSetter(order_id) {
return await axios.get(`http://site.local/calc-probs?order_id=${order_id}`)
}
const arr = [1, 2]
const promises = arr.map(n => proprobSetter(n))
Promise.all(promises).then(results => {
results.forEach((n, i) => {
console.log(`${i} = ${n}`)
});
})
When called, the async function returns a Promise.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question