Lazy evaluation

Isiah Meadows isiahmeadows at gmail.com
Thu Aug 31 13:21:29 UTC 2017


It'd be really nice if lazy values made it into the spec somehow. I've
already found myself using things like this [1] quite a bit, and I've
also found myself frequently initializing properties not on first
access.

[1]: https://gist.github.com/isiahmeadows/4c0723bdfa555a1c2cb01341b323c3d4

As for what would be a nice API, maybe something like one of these?

```js
class Lazy<T> {
    constructor(init: () => T);
    get(): T; // or error thrown
}

function lazy<T>(init: () => T): () => T; // or error thrown

function lazy<T>(init: () => T): {
    get(): T; // or error thrown
}
```

Alternatively, syntax might work, with `do` expression semantics:

```js
const x = lazy do { ... }
// expose via `x.get()` or just `x()`
```

-----

Isiah Meadows
me at isiahmeadows.com

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


More information about the es-discuss mailing list