inyansuta
7 months ago

Promise this context

Posted 7 months ago by inyansuta

How can I continue with Service after Promise resolve method called?

let Service = {
    load() {
        this.callServer("some/url")
            .then(response => {
                // this === undefined, but i need continue with Service
            })
            .catch(() => {
                //
            });
    },
    callServer(url, data = {}) {
        let self = this;
        return new Promise((resolve, reject) => {
            axios.post(url)
                .then(
                    $.proxy(function (response) {
                        // this === Service, ok
                        resolve(response);
                        // how can I bind "this" (Service) context to promise resolve?
                    }, self))
                .catch(function () {
                });
        });
    }
};

Please sign in or create an account to participate in this conversation.