Await top level promise

Hello,

For a Node/Bun script I need to await the top level promise:

let yow = async () => {
 	Console.log("doing promise stuff") 
}

yow()->Promise.done

compiles to

async function yow() {
  console.log("doing promise stuff");
}

yow();

but I would need this to be await yow(); for Bun to wait until the promise is resolved.
How can I do this?

You can just write await at the top level and it’ll work fine in Bun at least.

2 Likes

Oh, ok, this only works when your promise returns unit.
Mine was returning an array of unit. Thanks!

Oh TIL! Didn’t know this was possible, had a similar situation recently

1 Like

If you need to await a function that returns something, it’s common to assign it to _ to discard the return value

let _ = await promiseFn()

This will compile directly to await promiseFn(); which also works at the top level

1 Like