I frequently create promises to block some calls to class instances until some asynchronous initialization completes (see example code).
I've also noticed running a benchmark that a promise's .then()
call is fairly expensive, even though it's already resolved. A test function that ran ~50,000,000 calls per second was reduced to ~320,000 calls per second when wrapped in a resolved .then()
.
Is there a better practice of blocking calls much like with Promises?
Simple Example:
class SomeClass {
constructor() {
this.init_promise = new Promise(resolve => {
return fetch("some json")
.then(res => {
// Do things with data.
return resolve();
});
});
}
getSomething() {
return this.init_promise
.then(() => {
// Return something
});
}
}