Await Is Only Valid in Async Function

await is only valid in async function

The error is not refering to myfunction but to start.

async function start() {
....

const result = await helper.myfunction('test', 'test');
}

// My function

const myfunction = async function(x, y) {

return [

x,

y,

];

}

// Start function

const start = async function(a, b) {

const result = await myfunction('test', 'test');



console.log(result);

}

// Call start

start();

ASYNC / AWAIT SyntaxError: await is only valid in async functions and the top level bodies of modules

A Promise return something in the future so you need a way to wait for it.

// const CSVtoJSON = require("json2csv")
// const FileSystem = require("fs")
let temp = undefined;

async function test()
{
const data = await CSVtoJSON().fromFile('./input.csv')
// do your stuff with data

temp = data;

console.log(temp) // at this point you have something in.
return true
}

test();
console.log(temp) // at this point nothing was there.

Error in global call to async function: await is only valid in async functions and the top level bodies of modules ?

Yes, it is global code - in my script.js file. And I thought what could be more "top-level" than that?

As pointed out in a comment, the problem isn't "top level" it is "in a module".

Web browsers will load scripts as modules only if the type attribute says to do so:

<script type="module" src="script.js"></script>

This enables support for import (CORS permitting), makes the script load asynchronously, and stops the top level scope being global.

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.

How can i fix this error : SyntaxError: await is only valid in async function

Rule number one: Read the error.

What it tells you is that you can use the await keyword only inside an async function. So your piece of code must be wrapped like this:

async function run() {

const [coin, vsCurrency] = args;
try {
const { data } = await axios.get(`https://api.coingecko.com/api/v3/simple/priceids=${coin}&vs_currencies=${vsCurrency}`);
} catch (err) {
// Do something about the error.
}
}

// Don't forget to run your function
run();

Read this for a better understanding of async/await.

SyntaxError: await is only valid in async functions and the top level bodies of modules. Discord,js

You can use await with only async functions. Checkout the below code where I have changed your client.on callback function to async.

const client = require('../index.js');

client.on('ready', async() => {
let servers = await client.guilds.cache.size
let servercount = await client.guilds.cache.reduce((a,b) => a+b.memberCount, 0)

const activites = [
`?help | ${servers} servers`,
`Invite me now! | Watching ${servercount} members`
]

setInterval(()=>{
const status = activities[Math.floor(Math.random()*activities.length)]
client.user.setPresence({ activities : [{name : `${status}`}]})
}, 5000)


console.log(`✅ ${client.user.tag} is ready`)
})


Related Topics



Leave a reply



Submit