1

getRevenueByID function being called in async function, however getting an Uncaught SyntaxError. What am I doing wrong here?

(async() =>{

try {
    response = await fetch(mainURL);
    data = await response.json();

    console.log(data);
    console.log(data.results[0].title);

    ID_Array = [].concat.apply([], data.results.map(d => d.id))
    console.log(ID_Array);


    getRevenueByID(ID_Array);
} catch (error) {
    console.log(error);
}
})();

getRevenueByID = (arr => {
    for (let i = 0; i < arr.length; i++){
        console.log("ID is: ", arr[i]);
        getRevenueURL = await fetch('someurl' + arr[i] + '?api_key=YOUR_KEY&language=en-US');
        console.log(getRevenueURL);
        // let data = await getRevenueURL.json();
        // console.log(data);

    }
});
2
  • 3
    You can only await a function call from directly inside an async function. Commented Feb 25, 2019 at 15:58
  • You should get reference error. getRevenueByID is not defined at the time you call the function. Commented Feb 25, 2019 at 16:04

1 Answer 1

3

getRevenueByID itself is not an async function.

"await is only valid in async function" means "directly in" not "called from somewhere back in the callstack".

So make it async:

getRevenueByID = async (arr) => {
    // ...
};

And then await it back where you call it:

await getRevenueByID(ID_Array);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.