Skip to content

Instantly share code, notes, and snippets.

@lmaccherone
Created February 27, 2023 20:42
Show Gist options
  • Save lmaccherone/c2384f499052f75a20c2b2b5e9f250e5 to your computer and use it in GitHub Desktop.
Save lmaccherone/c2384f499052f75a20c2b2b5e9f250e5 to your computer and use it in GitHub Desktop.
hydrate() {
if (this.hydrated) {
return Promise.resolve()
}
if (this._hydrating) {
return this._hydrating
}
// This assigns the promise SYNCHRONOUSLY within the method call, which
// is important for re-entrancy. If we await()ed anything above this point,
// we could have a race.
return this._hydrating = (async () => {
// NOW we can do async work.
await whatever()
this.hydrated = true;
})().finally(() => {
// After finishing, clear out the promise.
this._hydrating = null
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment