Re: [WICG/webcomponents] Lazy Custom Element Definitions (#782)

@EisenbergEffect 

> The platform could invoke the callback when it first sees the element, but the lazy callback could choose when it wants to actually define it.

This is already possible with promises. You can wait for arbitrary things before returning a class. Anything you can do with an such an object you can do with returning a Promise.

I personally like coalescing on fewer async primitives like promises. An object that can be used to define an element is not so different from a callback, and I _strongly_ prefer Promise-based APIs over callback-based ones because of better composition and integration with the language.

I think promises give a nice API for the simples case of a default export:

```ts
customElements.lazyDefine('my-element, () => import('my-element'));
```

Pretty nice API for named exports:

```ts
customElements.lazyDefine('my-element, async () => (await import('my-bundle')).MyElement);
```

And you can do arbitrary async computation if you need:

```ts
customElements.lazyDefine('my-element, async () => {
  const module = await import('my-module');
  const myClass = await computeMyClassSomewhow(module);
  return myClass;
});
```



-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/782#issuecomment-1523714899
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/issues/782/1523714899@github.com>

Received on Wednesday, 26 April 2023 16:28:36 UTC