kaleidawave / ezno

A JavaScript compiler and TypeScript checker written in Rust with a focus on static analysis and runtime performance
https://kaleidawave.github.io/posts/introducing-ezno/
MIT License
2.3k stars 42 forks source link

Promises, async and await #168

Open kaleidawave opened 2 weeks ago

kaleidawave commented 2 weeks ago

Currently Promise type annotations and awaiting them does work. But that is it

One thing currently stuck on is how to represent the flow in a async situation

async function func(something: Promise<T>) -> Promise<[T]> {
    const x = await new Promise((res, rej) => res(2));
    // this returns when `res` is called

    await new Promise((res, rej) => setTimeout(res, 1000));
    // this runs when setTimeout calls res

    const y = await something;

    return [y]
}

let x = 0;

async function immediate() {
  x = 1
}

x satisfies 0;
immediate(); // (btw await has no effect here, unlike Rust)
x satisfies 1

/// On the other hand
let x = 0;

const wait = new Promise((res, rej) => {
  setTimeout(res, 1000);
}).then((_) => {
  x = 1;
});

x satisfies 0;
await wait; // Does have an effect. If not `await`, then the next == 0
x satisfies 1;

Thinking

Additionally