V
V
Vanik Khachatryan2018-03-31 19:44:02
JavaScript
Vanik Khachatryan, 2018-03-31 19:44:02

Why does axios with async return a promise object?

async getTickets(slot_id) {
                let tickets;
                await axios.get('/api/tickets/' + slot_id)
                    .then(({data}) => {
                        tickets = data.data;
                    });

                return tickets;
            }


I receive a Promise object, and only it has in which my data is stored that I returned in the function, how can I immediately receive this data without a promise object?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2018-03-31
@VaniXac

What do you expect with this code?
This is how it should be (roughly):

async getTickets(slot_id) {
  try {
    const { data } = await axios.get('/api/tickets/' + slot_id)
    return data
  } catch (e) {
     throw new Error(e)
  }
}

https://jsfiddle.net/yarkov_aleksei/kkdyzg1m/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question