Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign data returned from $promise to global variable

I've two services, service1 and service2, and I would like to invoke a method of service1 into service2.

Ideally, I would like to assign the returned data from service1.getMethod() to a global variable declared as var result = [].

Here is the code:

service1

.factory('service1', function (dependencies...) {
    var getMethod1 = function () {
       ...//making http get call
       return deferred.promise();
    };

    return {
       getMethod1 : getMethod1
    };
});

service2

.factory('service2', function (dependencies...) {
    var result = [];

    var getMethod2 = function () {
        Service1.getMethod1().then(function (data) {
            result = data;
    });

    //RUN METHOD
    getMethod2();

    //Here ideally, I would like result to contain `[object, object, object]`
    console.log(result); //instead it prints `undefined`
});

So ideally, I would like to use what's will be in result in service2's other functions i.e. result[0].name etc. Not sure if what I'm doing is the right approach.

Please provide an plunker demo or code snippet example and if not sure about something, please write comment below.

Thanks!

like image 661
Simple-Solution Avatar asked Nov 27 '14 20:11

Simple-Solution


People also ask

Can you assign promise data value to outside variable?

variable will not be defined until the promises resolve, and generally speaking the only way to know when that is is to only access it from within the then clause. If you can't do that (such as when it needs to be referenced from a template), it's generally easiest to initialize it to a dummy value first.

Can we return a global variable by reference?

We can return a global variable by reference. We cannot return a local variable by reference.

Can you change global variables?

Functions can access global variables and modify them. Modifying global variables in a function is considered poor programming practice. It is better to send a variable in as a parameter (or have it be returned in the 'return' statement).

How do you wait for async to finish?

Await: Await function is used to wait for the promise. It could be used within the async block only. It makes the code wait until the promise returns a result. It only makes the async block wait.


1 Answers

You can't work with asynchronous code like you are attempting. result variable is simply not yet populated when you are trying to use it. Instead you should make getMethod2 return promise too, and use its then method:

.factory('service2', function (dependencies...) {

    var getMethod2 = function () {
        return Service1.getMethod1();
    };

    // RUN METHOD
    getMethod2().then(function(result) {
        console.log(result);
    });
});

You can also cache returned data:

.factory('service2', function (dependencies...) {

    var result;

    var getMethod2 = function () {
        return result ? $q.when(result) : Service1.getMethod1().then(function(data) {
            result = data;
            return result;
        });
    };

    // RUN METHOD
    getMethod2().then(function(result) {
        console.log(result);
    });
});
like image 150
dfsq Avatar answered Sep 25 '22 17:09

dfsq