0

I have these two functions and this is a sample of code

var getRangeTime = function(numSerie){
      $http.get("data/tempsgammes.json").then(function(res){
          return $scope.time = res.data[numSerie].temps
    })}

var updateScreen = function() {
      $http.get("http://localhost:5000").then(function(response){
        $scope.numSerie = response.data.refDetail.Num_Serie
        $scope.numTeam = response.data.team
        $scope.t = getRangeTime($scope.numSerie)
        //Rest of code the code
    }

Here what I want to do :

First of all, I call the function updateScreen() to get the Serial Number (numSerie), then depending on the value of numSerie, another function getRangeTime() will be exectued to get the time, and then the function updateScreen() will continue her execution using the result of getRangeTime().

The problem here is the asynchronous, I mean the updateSreen() should wait getRangeTime() until she returns the desired value, it's kind of async and await.

I've tried it but it didn't work and actually I don't know how to use them very much, I searched here and I tried the existing solutions but it didn't go well too, I got always the undefined.

Can anyone help me ?

2 Answers 2

2

In this case you should use promise chains to call the request one after the other.

function updateScreen(){
    return $http.get("http://localhost:5000")
}

function getRangeTime(){
   return  $http.get("data/tempsgammes.json")
}

$scope.calltwoFunctions = function() {
     updateScreen()
       .then( function( response )
        {
            $scope.numSerie = response.data.refDetail.Num_Serie
            $scope.numTeam = response.data.team
            return getRangeTime();
        })
        .then(function(response){
           console.log(response.data);
        })
}
Sign up to request clarification or add additional context in comments.

1 Comment

okay thank you so much, i'll try it and then I will comment another time.
0

A little late here but you can also use callbacks(I haven't tested this but it should work):

var updateScreen = function(callback) {
   $http.get("http://localhost:5000").then(function(response){
        $scope.numSerie = response.data.refDetail.Num_Serie
        $scope.numTeam = response.data.team
        $scope.t = getRangeTime($scope.numSerie)
        callback($scope.numSerie);
    }
}

updateScreen(function(numSerie) {
   $http.get("data/tempsgammes.json").then(function(res){
          return $scope.time = res.data[numSerie].temps
    }
})

2 Comments

Thank you so much for your answer, I tried the solution provided by @Sajeetharan and it works.I'll try yours in another time. But I have a question related to "Sajeetharan" if you can answer it, after calling return getRangeTime(); I complete the rest of the code in the second response, isn't it ?
@AnesElGongi Yes, by the looks of it, that would be my understanding as well.

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.