react-concurrent-router
Version:
Performant routing embracing React concurrent UI patterns
33 lines (30 loc) • 916 B
JavaScript
'use strict';
class SuspendableResource {
constructor(loader, isModule = false) {
this.load = () => {
if (this._result) return this._result;
if (this._promise) return this._promise;
this._promise = this._loader().then(result => {
const returnValue = this._isModule ? result.default || result : result;
this._result = returnValue;
return this._result;
}).catch(error => {
this._error = error;
});
return this._promise;
};
this.isLoaded = () => Boolean(this._result);
this.read = () => {
if (this._result) return this._result;
if (this._error) throw this._error;
if (this._promise) throw this._promise;
throw this.load();
};
this._isModule = isModule;
this._loader = loader;
this._promise = null;
this._result = null;
this._error = null;
}
}
module.exports = SuspendableResource;