`Is it even possible to return a result in an async function?
V
V
Vika Marmeladka2021-09-22 16:46:43
JavaScript
Vika Marmeladka, 2021-09-22 16:46:43

Is it even possible to return a result in an async function?

I have a function:

async function RenderCurrencies() {
    const API_KEY = 'ea1ecb2431c7cc30724620c2b4c2fc24'
    const response = await fetch(`http://api.exchangeratesapi.io/v1/latest?access_key=${API_KEY}`)

    return response 
}

let newVar = RenderCurrencies()

Similar questions were asked, but they were all with the question of whether it is possible to return using just return. As I understand it, this is not possible.
But are there any other options for the function to make a request to the server and return a response, and I get the result in the newVar variable?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-09-22
@homsi959

let newVar = await RenderCurrencies();

N
Nadim Zakirov, 2021-09-22
@zkrvndm

You have an error, it will be correct like this:

async function RenderCurrencies() {
    const API_KEY = 'ea1ecb2431c7cc30724620c2b4c2fc24'
    const response = await (await fetch(`http://api.exchangeratesapi.io/v1/latest?access_key=${API_KEY}`)).text();
    return response;
}

let newVar = await RenderCurrencies();
console.log(newVar);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question