winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
4.78k stars 189 forks source link

Await/Defer Statement #116

Open staycoolcall911 opened 1 year ago

staycoolcall911 commented 1 year ago

The following section should be added to the language reference:

Contrary to JavaScript, any call to an async function is implicitly awaited in Wing. As a result, await in Wing is a rarely used keyword, since its use is implied by the inflight keyword. await is only used when you want a defered Promise to be fulfilled before execution flow continues. The Wing compiler emits awaits when encountering Promise<T> types as r-values in expressions. Use the defer keyword to defer the resolution of a promise and obtain a Promise<T> type instead (a.k.a unawait what the compiler does).

"defer" prevents the compiler from awaiting a promise and grabs a reference.
"await" and "Promise" are semantically similar to JavaScript's promises.
"await" statement is only valid in inflight function declarations.
Awaiting non promises in Wing is a no-op just like in JavaScript.

// Wing program:
class MyClass {
  inflight foo(): num {
    let w = defer somePromise();
    let x = await w;
    return x;
  }
  inflight boo(): num {
    let x = somePromise();
    return x;
  }
}
github-actions[bot] commented 1 year ago

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!