-1

I am making get request in node.js for an id which i would like to use in an other function. The problem is the second function starts before first fetches the id.

const getUsersID = async (username) => {
    let config = {
        method: 'get',
        url: `https://www.instagram.com/${username}/?__a=1`,
        headers: {...}
   };
    axios(config)
    .then((response) => {
        const id = response.data.graphql.user.id;
        console.error(id)
        return id
    })
    .catch((err) => {
        console.log(err);
    });
}

const getUsersPosts = async (userID) => {
    const options = {
        method: 'get',
        url: `https://api.instagram.com/v1/users/${userID}/media/recent/?access_token=${accessToken}`
    }
    axios(options
    ).then(res => console.log(res.data)
    ).catch(err => console.error(err))
}

I tried this two styles of calling them in the main function but both return the same result in terminal.

const userID = await getUsersID('therock')
console.log(userID)
await getUsersPosts(userID)

// and this one
getUsersID('therock').then(id => {
    console.log(id)
    getUsersPosts(id)
}).catch(err => {
    console.error(err)
})

The terminal returns:

undefined
err
232192182
Dane Brouwer
  • 2,827
  • 1
  • 22
  • 30
  • 2
    Neither `getUsersID` nor `getUsersPosts` does `return` anything. Also [avoid using `.then()` calls when you can use `await` syntax](https://stackoverflow.com/a/54387912/1048572)! – Bergi Aug 12 '21 at 08:48
  • It returns the id in the .then(). I also can clg it but if you look at the terminal it is printed after the second function already failed. Also why is await better than .then()? –  Aug 12 '21 at 08:51
  • 1
    That `return`s from the callback, not from `getUsersID` or `getUsersPosts`. The promise that `.then()` produces is ignored. – Bergi Aug 12 '21 at 08:52
  • oh okay i get it thank you so much you have helped me a lot –  Aug 12 '21 at 08:54
  • 1
    _"It returns the id in the .then()..."_ - Yes, but you're `await`ing the return value of `getUsersID()` and there's none (not literally) because you don't return anything from `getUsersID()` – Andreas Aug 12 '21 at 08:54

1 Answers1

0

You have not return the result at getUsersID.

const getUsersID = (username) => {
    let config = {
        method: 'get',
        url: `https://www.instagram.com/${username}/?__a=1`,
        headers: {...}
   };
    return axios(config)
      .then((response) => {
        const id = response.data.graphql.user.id;
        return id
    })
      .catch((err) => {
        console.err(err);
    });
}

const getUsersPosts = (userID) => {
    const options = {
        method: 'get',
        url: `https://api.instagram.com/v1/users/${userID}/media/recent/?access_token=${accessToken}`
    }
    return axios(options)
      .then(res => console.log(res.data))
      .catch(err => console.error(err))
}

If you want to use async/await, try this

const main = async () => {
  const userID = await getUsersID('therock')
  console.log(userID)
  await getUsersPosts(userID)
}
main();
cookie
  • 69
  • 6