john samcock
john samcock

Reputation: 137

How to pass promise value into a function

I have a piece of angular code here below.

I need to pass a value i get from a promise where it says **value** in the code

Here is my promise below

Auth.loggedInUser().then(function (user){
  return user.id
}); 

Below is angular code

.run(['$rootScope', 'ngCart','ngCartItem', 'stores', function ($rootScope, ngCart, ngCartItem, stores) {

    $rootScope.$on('ngShoppingCart:changed', function(){
        ngCart.$save();
    });

    if (angular.isObject(stores.get(**value**))) {
        ngCart.$restore(stores.get(**value**));

    } else {
        ngCart.init();
    }

}])

How can i pass what is returned (the value) in the async function up above into where it says value in the angular code? Not sure of the proper method.

Upvotes: 0

Views: 66

Answers (2)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41447

Inject the auth service to run and use the regular expression inside the promise

.run(['$rootScope', 'ngCart', 'ngCartItem', 'stores', 'Auth', function($rootScope, ngCart, ngCartItem, stores, Auth) {

    $rootScope.$on('ngShoppingCart:changed', function() {
        ngCart.$save();
    }); 

    Auth.loggedInUser().then(function(user) {

        if (angular.isObject(stores.get(user.id))) {
            ngCart.$restore(stores.get(user.id));

        } else {
            ngCart.init();
        }
    });

}])

Upvotes: 0

Jaromanda X
Jaromanda X

Reputation: 1

Perhaps this will work

.run(['$rootScope', 'ngCart','ngCartItem', 'stores', function ($rootScope, ngCart, ngCartItem, stores) {

    $rootScope.$on('ngShoppingCart:changed', function(){
        ngCart.$save();
    });
    Auth.loggedInUser().then(function (user){
        if (angular.isObject(stores.get(user.id))) {
            ngCart.$restore(stores.get(user.id));

        } else {
            ngCart.init();
        }
    }); 

}])

Upvotes: 2

Related Questions