[proposal] Persistent variables in functions/methods

Neek Sandhu neek.sandhu at outlook.com
Tue Jul 17 06:26:00 UTC 2018

Ok maybe you guys are impressed enough 😊

Here’s a 339 line file and [on Line 85 there’s a variable called `workerInitPromise`](https://github.com/NeekSandhu/v-thread/blob/ed350bd4dcbf8e368cea2b3819f405b61ef783a3/src/MainThread.ts#L85)

Only one method in that class is concerned with `workerInitPromise`, that is `startWorker()`

What is `workerInitPromise`?

Well, whenever [`startWorker()`](https://github.com/NeekSandhu/v-thread/blob/ed350bd4dcbf8e368cea2b3819f405b61ef783a3/src/MainThread.ts#L236) is called it should return a `Promise` that should resolve when the `Worker` is up and running.

That means one call to `startWorker` has started the worker startup sequence and it’d be wasteful to start it over again when someone else calls `startWorker`.

Instead `startWorker` decides to “share” the `Promise` amongst furious callers. Now, `startWorker` needs a place to store that `Promise` so he can share with subsequent callers.
Where is that place???

And that my friends is why I had to create `workerInitPromise` prop on the class, just to make `startWorker` happy.

It “belongs” in `startWorker` and should “live” inside `startWorker`

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180717/583bd4bd/attachment.html>

More information about the es-discuss mailing list