let fooServiceInstance = null;
export default class FooService {
constructor() {
if (fooServiceInstance) {
return fooServiceInstance;
}
fooServiceInstance = this;
}
loadFoo() {
return fetch();
}
getFoo(forceReload) {
if (this.foo && !forceReload) {
return Promise.resolve(this.foo);
}
return this.loadFoo()
.then(foo => this.foo = foo)
.then(() => this.foo);
}
}
import S from './s';
const s1 = new S();
const s2 = new S();
s1.getFoo().then(() => s2.getFoo())
Add a code snippet to your website: www.paste.org