How to resolve the Syntax error : await is only valid in async function?

await can only be called in a function marked as async.
so, you can make an async IIFE and call the httpGet from there.

(async function(){
    var body = await httpGet('link');
    $.response.setBody(body);
})()

Basically when you use one asynchronous operation, you need to make the entire flow asynchronous as well. So the async keyword kindof uses ES6 generator function and makes it return a promise.

You can check this out if you have confusion.

Leave a Comment