M
M
My joy2021-07-01 12:32:34
JavaScript
My joy, 2021-07-01 12:32:34

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}`)
});


outputs:
1 = [object Promise]
2 = [object Promise]


What am I doing wrong? tell me the correct option.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WapSter, 2021-07-01
@t-alexashka

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}`)
  });
})

D
display: block, 2021-07-01
@qork

When called, the async function returns a Promise.

https://developer.mozilla.org/en/docs/Web/JavaScript...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question