V
V
Vika Marmeladka2021-05-16 11:21:34
AJAX
Vika Marmeladka, 2021-05-16 11:21:34

Why can't I return data from an AJAX function?

I have an input on which the event lister is hung to change the input, a callback is launched in which my RequestForCadastralCode function is launched.

But I constantly get undefined, I'm sure the problem is that I'm not using async await correctly, but I don't know how to solve this problem.

Here is the code https://jsfiddle.net/1a65yxo2/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zav19791979, 2021-05-16
@homsi959

when you use async in

let arrCities = async () => {
      	return await RequestForCadastralCode(e.target.value)
      }

you say that the arrCities method will return a promise, await is working inside it, which is equivalent to using then in ordinary promises. But the method itself will return exactly the promise and not the result of the promise. In order to get the result and use await as you are trying to do - you need to mark the async method itself in the lissener.
you also forgot retern in the RequestForCadastralCode method, if you do not use it, then the request will be executed, but its result will not be returned.
here is your working code https://jsfiddle.net/zav19791979/mkywn1zb/2/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question