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