4

I execute a function that calls two functions sequentially, to run the second function should first finish the first one. But this does not happen, perhaps because the first function is asynchronous. I read that I need to use "promise" I tried it, in different way, but it doesn't works. So I rewrote the function as I initially wrote:

function fail() { 
    // Get the snackbar DIV
    var x = document.getElementById("snackbar")

    // Add the "show" class to DIV
    x.className = "show";

    // After 3 seconds, remove the show class from DIV
    setTimeout(function(){ x.className = x.className.replace("show", "");}, 3000);   

}

function _goback(){
    $location.url('/app/dispensers');
}    

//Check if the item is still available (if nobody bought it)
function _checkavailability(response){
    if (response.data == ""){
              console.log("Accesso non autorizzato")
    }
    $scope.infoproductbyid = response.data;
    if($scope.infoproductbyid.purchaseTime == null){
        console.log("Item disponibile");
        //if the item is available, it's possible to proceeds to checkout
        $location.url('/app/notregcheckout');
    }
    else{
        console.log("Spiacente, item non più disponibile");
      //  localStorage.clear("tokenidproduct");
        //_showSB();  
        fail();
        _goback();
    }    
}               

In the last rows, you can see that I call fail() function for first, and _goback() function for second. I want that _goback()starts when fail()finish, but fail() contains a timeout, and I think that for this reason the function is asynchronous. I don't understand how I can do

10
  • You can put _goback(); function inside of fail() function time out function like setTimeout(function() { x.className = x.className.replace("show", ""); _goback(); }, 3000); Commented Jun 4, 2017 at 17:37
  • I already tryed in this way; ' setTimeout(function(){ x.className = x.className.replace("show", ""); _goback();}, 3000);' but in this case _goback() doesn't works Commented Jun 4, 2017 at 17:38
  • THe second function doesn't start in this way Commented Jun 4, 2017 at 17:39
  • Did you get any error? Commented Jun 4, 2017 at 17:39
  • Then put another setTimeout like setTimeout(function() { x.className = x.className.replace("show", ""); setTimeout(function(){ _goback(); },0) }, 3000); Commented Jun 4, 2017 at 17:41

1 Answer 1

6

Use the $timeout service to create a promise:

function fail() { 
    // Get the snackbar DIV
    var x = document.getElementById("snackbar")

    // Add the "show" class to DIV
    x.className = "show";

    // After 3 seconds, remove the show class from DIV
    var promise = $timeout(function() {
      x.className = x.className.replace("show", "");
    }, 3000);

    //RETURN promise   
    return promise;
}

Then use the .then method to wait:

fail().then(function() {
    _goback();
});
Sign up to request clarification or add additional context in comments.

2 Comments

' $timeout is not defined'
ok, I injected $timeout in the function of the controller: .controller("CartController", function($scope, $filter, $http, $location, $stateParams, restService, $timeout) { Thank you, it works fine now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.