None
None

Reputation: 9247

How to create promise?

Problem is that $window.print() is always called and not just when promise is success.How can i create promise when data is populated ? Any suggestion?

'use strict';
angular.module("printModule").controller('printController', ['$scope', '$window', '$q', function ($scope, $window, $q) {


    $scope.ticketPin = localStorage.getItem("pin");
    $scope.payoutTime = localStorage.getItem("payoutTime");
    $scope.payoutAmount = localStorage.getItem("payoutAmount");

    var defer = $q.defer();
    defer.resolve($scope.ticketPin);
    defer.resolve($scope.payoutTime);
    defer.resolve($scope.payoutAmount);

    defer.promise.then(function () {
        $window.print();
    })
}]);

Upvotes: 1

Views: 92

Answers (1)

tuckerjt07
tuckerjt07

Reputation: 922

function getFromLocalStorage (item, callback)  {
    //To prevent errors
    if (callback) {
        return callback(localStorage.getItem(item)) ;
    }
} 

var callback;
callback = function (data) {
    //Set variable to data
} 

//I would make getFromLocalStorage a factory
myFactory.getFromLocalStorage("itemName", callback);

Caveat, the above should be able to be made to work but I have not used LocalStorage enough to know the ins, outs, and gotchas, so there may need to be some rearrangement of that nested return.

Upvotes: 1

Related Questions