Created
February 27, 2023 20:42
-
-
Save lmaccherone/c2384f499052f75a20c2b2b5e9f250e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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