K
K
komigor2021-07-12 07:49:45
JavaScript
komigor, 2021-07-12 07:49:45

Why does a method return undefined during recursion?

Here I have a method in a class and for some reason it returns undefined during recursion .

async getDogApi() {
    try {
      const result = await axios.get('https://random.dog/woof.json');
      const posts = result.data;
      const format: string = posts.url.substr(posts.url.length - 4);
      if (format != '.jpg' /*|| '.PNG' || '.JPG' || '.png' || 'JPEG' || 'jpeg'*/) {
        await this.getDogApi();
      } else {
        console.log(posts)
         return posts;
}
    } catch (err) {
      return null;
    }


If recursion does not occur, then everything is in order, but if it does, then here we get undefined? I wonder why
console.log(await apiService.getDogApi());

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2021-07-12
@komigor

return await this.getDogApi();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question