HarryBilney
HarryBilney

Reputation: 45

Axios Get Function Returning Pending Promise

I am getting data from an API using Axios within NodeJS. This is my code for that:

async function getJobsToPrep() {
    const promise = await axios({
        method: "GET",
        url: END_POINT + `/opportunities/`,
        headers: {
            "X-SUBDOMAIN": "premier-production",
            "X-AUTH-TOKEN": "******"
        },
        params: {
            view_id: "1000061"
        }
    })
    return promise.data.opportunities
}

I've hidden my auth token for security. If I console.log the promise.data within the function, I get the result I am expecting, but if I try and get the returned data from elsewhere using an async await function like this:

console.log(jobsToPrep = async () => await getJobsToPrep())

I get this response

[AsyncFunction: jobsToPrep]

I'm unsure why it is just telling me that jobsToPrep is just an async function and not giving me the returned values from the function.

Any ideas much apricated.

Upvotes: 0

Views: 690

Answers (1)

Nicholas Tower
Nicholas Tower

Reputation: 84902

jobsToPrep = async () => await getJobsToPrep()

This means "create a function with the text async () => await getJobsToPrep() and assign it to the variable jobsToPrep". You then log out that function -- not the result of the function, the function as a whole.

Perhaps you meant to do this:

const jobsToPrep = await getJobsToPrep();
console.log(jobsToPrep)

Upvotes: 1

Related Questions