1

I use service like this :

province(){
        return this.$http({
            method : "POST",
            url : "http://192.168.1.38:3000/provinces/_search"
        });
    };

and in controller get data like this :

comboInit(){

        /*Province*/
        this.initInfo.province().then(
            /*success*/ (result) => {
                console.log(result);
                this.province=result.data;
            },
            /*error*/(err) => {
                console.log(err);
            }
        );
    }

but I want call province() method like this

this.province=this.initInfo.province()

I want this.initInfo.province() method without .then() or success() in controller

Reza Mazarlou
  • 2,986
  • 4
  • 22
  • 31
  • 1
    What makes you think you can do that when what you are returning from the function is a promise? What higher level problem are you trying to solve? – charlietfl Feb 26 '17 at 14:25
  • Have a look at method chaining: `https://schier.co/blog/2013/11/14/method-chaining-in-javascript.html` – Gerrit van Huyssteen Feb 26 '17 at 14:33
  • There's only 2 options. One is to use `then`, and it isn't bad at all, that's what we usually do. Another is to use $resource, it returns self-filling object. – Estus Flask Feb 26 '17 at 16:26
  • If the app uses a router, return the promise in a resolve function. The router will wait for the promise to resolve before creating the view. – georgeawg Feb 26 '17 at 19:12
  • JavaScript is single-threaded. A function can not return data that is not available. It can only return a *pending* promise which is resolved at a later time or an *empty* reference that is later populated with data from the server. – georgeawg Feb 26 '17 at 20:21

0 Answers0