1

So I've got two functions. The first function calls the second. The console.log(listDogs) appears to show an object. the console.log(listDogs2) shows as undefined. The idea outcome is that the return from the function is the same as before.

async function listDogs() {
    let listDogs2 = await getListDogs();
    console.log(listDogs2);
}

async function getListDogs(){
    await axios.get(`https://dog.ceo/api/breeds/list/all`)
    .then(response => {
        let listDogs = response.data.message;
        console.log(listDogs);
        return listDogs;
    }).catch((err) => {
        console.log(err);
    });
}

Console Output
line 31 is console.log(listDogs);
line 7 is console.log(listDogs2);

Console output

Any help appreciated. Either a solution, or a pointer about where to read. Have done quite abit of searching and cannot get my head round it. Thanks in advance.

bob marley
  • 70
  • 7
  • 1
    The `getListDogs` function doesn't actually return anything. You need to `return` the result of `axios.get()`. (Since the function is `async` you can simplify it a lot by getting rid of the `.then()` and `.catch()` callbacks and just `await`-ing the result of `axios.get` and returning that result.) – David Apr 08 '22 at 20:54
  • 1
    So: `async function getListDogs() { const listDogs = await axios.get('...'); return listDogs.data.message; }` Wrap it in a try/catch if you want to catch errors. – David Apr 08 '22 at 20:55
  • Thanks @David. Still getting my head around functions created within functions with no name. All makes sense cheers! – bob marley Apr 08 '22 at 21:05

0 Answers0