Using Await Outside of an Async Function

Using await outside of an async function

Top level await is not supported. There are a few discussions by the standards committee on why this is, such as this Github issue.

There's also a thinkpiece on Github about why top level await is a bad idea. Specifically he suggests that if you have code like this:

// data.js
const data = await fetch( '/data.json' );
export default data;

Now any file that imports data.js won't execute until the fetch completes, so all of your module loading is now blocked. This makes it very difficult to reason about app module order, since we're used to top level Javascript executing synchronously and predictably. If this were allowed, knowing when a function gets defined becomes tricky.

My perspective is that it's bad practice for your module to have side effects simply by loading it. That means any consumer of your module will get side effects simply by requiring your module. This badly limits where your module can be used. A top level await probably means you're reading from some API or calling to some service at load time. Instead you should just export async functions that consumers can use at their own pace.

await used outside an async function

If I'm not wrong await can be only used in async function. But you can use .then(() => {}) and add logic in it, you will be sure that the code will be triggered after the promise and your data will be defined.

In your case I guess you want something like this

getDocs(query(collection(db), 'persons')))
.then((foo) => {
// do something foo is defined
// code outside this scope will be triggered before the .then
})
.catch(err => console.log("there is an error", err));

Remember that async function will always return a promise and so even you you wrap your code inside async function you will have to use a .then() at the end. So maybe you want to stay with .then instead because you don't need async function to use .then

Is it possible to use await outside async function

You can't use await outside an async function.
one trick to 'bypass' this limit is to use async IFEE:

const myFunction = async() => {
const exercises = await Exercise.find({ workoutId })
return exercises
};

(async () => {
const value = await myFunction()
})()

line 25 SyntaxError: 'await' outside function Pyscript

Your have multiple problems with your code.

1) Import asyncio

This package is required for PyScript applications that use async functions.

import asyncio

2) Calling Async functions

Your code is using the await with in a function that is not declared async. Change your code:

async def init():
uploadButton.on_click(process_file)
await show(fileInput,'fileinput')
await show(uploadButton,'upload')
await show(to_pred,'to_predict')

init()

3) Undefined element

Your code is referencing an undefined element to_predict. I am not sure that that is, but you will need to add that element to your code.

await show(to_pred,'to_predict')

SyntaxError: 'await' outside async function

discord's api uses an asynchronous library, so it calls the library's functions with 'await.' that fetches the data you're using from the discord server. you use this with 'async def' when you're writing a command that goes through the client. the reason you're getting that error is because you're trying to use 'await' in a regular python function (defined with 'def' instead of 'async def.') it doesn't work because there's nothing to await, and as it's a regular (synchronous) function instead of an async function, you won't be able to call it in discord, and since you cant call it there won't be any context to send the message to.

for what you're trying to do, i suggest changing it from 'def' to 'async def' and adding the @commands.command(), then fiddling with it from there. sometimes it takes a while to get right, but more often than not you'll get there in the end!

How to deal with 'await' outside async function?

Hope this helps. Example of calling async function from regular function:

import asyncio

loop = asyncio.get_event_loop()

async def demo(name):
return f"hello {name}"

def main():
result = loop.run_until_complete(demo("world"))
print(result)

if __name__ == '__main__':
main()

Because I can't run await on the top level, I have to put it into an async function - why can I then call that async function directly?

Top-level await used to not be a thing, but it is possible now in ES6 modules.

One reason why top-level await used to not be a thing, and is still not a thing outside of modules is that it could permit syntactical ambiguity. Async and await are valid variable names. outside of modules. If a non-module script permitted top-level await, then, short of re-working the specification (and breaking backwards compatibility), there would be circumstances when the parser couldn't determine whether a particular instance of await was a variable name, or was used as the syntax to wait for the Promise on its right-hand side to resolve.

To avoid any possibility of ambiguity, the parser, when parsing a section of code, essentially needs to have flags that indicate whether await is valid as an identifier at any given point, or whether it's valid as async syntax, and those two must never intersect.

Module scrips permit top-level await (now) because the use of await as an identifier has always been forbidden in them, so there is no syntactical ambiguity.

In contrast, there are zero issues with using .then on the top level because it doesn't result in any ambiguity in any circumstances.

Why doesn't it just return a Promise which is never executed because it doesn't get awaited?

Promises aren't really "executed". They can be constructed, or waited on to fulfill, or waited on to reject. If you have a Promise, you already have some ongoing code that will (probably) eventually result in a fulfillment value being assigned to the Promise.

Hanging Promises are syntactically permitted - values that resolve to Promises but which aren't interacted with elsewhere. (Which makes sense - every .then or .catch produces a new Promise. If every Promise had to be used by something else, you'd end up with an infinite regress.)

Doing

(async () => {
foo = await someOtherAsyncFunc();
console.log(foo);
})();

is essentially syntax sugar for

someOtherAsyncFunc()
.then((foo) => {
console.log(foo);
});

There's no need to tack anything else onto the end of either of those. (though it's recommended to add a .catch to a dangling Promise so unhandled rejections don't occur)

Why does this await outside async function work?

Seems like your environment supports the top-level await proposal. The proposal is currently at stage 4 which means "finished" and can be included in the specs officially. But support may still vary.

Quoting the proposal:

Top-level await lets us rely on the module system itself to handle all of these promises, and make sure that things are well-coordinated. The above example could be simply written and used as follows:

// awaiting.mjs
import { process } from "./some-module.mjs";
const dynamic = import(computedModuleSpecifier);
const data = fetch(url);
export const output = process((await dynamic).default, await data);
// usage.mjs
import { output } from "./awaiting.mjs";
export function outputPlusValue(value) { return output + value }

console.log(outputPlusValue(100));
setTimeout(() => console.log(outputPlusValue(100), 1000);

None of the statements in usage.mjs will execute until the awaits in awaiting.mjs have had their Promises resolved, so the race condition is avoided by design. This is an extension of how, if awaiting.mjs didn't use top-level await, none of the statements in usage.mjs will execute until awaiting.mjs is loaded and all of its statements have executed.

Because you have a top-level await in database.js then services.js will only execute when that module has had its client promise resolved.



Related Topics



Leave a reply



Submit