Welcome, guest! Login / Register - Why register?

Paste

Pasted as JavaScript by x ( 3 years ago )
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())

 

Revise this Paste

Your Name: Code Language: