2

I have two functions is_exist() and save() functions. In save function I calls is_exist() function before performing a save.it return boolean value. but my problem is save() function is not wait until is_exist() function to return the value.after is_exist() function called it just jump into next line and continue execute bellow lines, after save() finished is_exist() function return the value

var app = angular.module('my_app', [])
app.controller('my_con', ['$scope', '$http', function($scope, $http) {

    $scope.save = function(sector) {
        var del_status = $scope.is_exist(sector);

        console.log(del_status); // is_exsis return value

        if(!del_status) {
            // save
        }
    }

    $scope.is_exist = function(sector) {
        $http({
            method : 'POST',
            url : '/is_data_exist',
            contentType : 'application/json; charset=utf-8',
            data : angular.toJson({
                'sector': sector
            }),
            dataType: 'json',
        }).then(function successCallback(response) {

            console.log(response.data); //http respond

            if(response.data == 'False') {
                return false;
            }
            else {
                return true;
            }
        })
    }
}])

is there any way to wait until is_exsis() function to return value and then go to next line

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • it is asynchronous, it means you "can't", because you don't know when the promise will be resolved. What you can do is pass the following code as a function to your promise, so that the promise executes it. something like `var del_status = $scope.is_exist(sector, function(){ console.log(del_status); /* etc */ });` (be careful, you have a typo, exist != exsis) – Kaddath Jun 12 '17 at 11:45
  • duplicate : https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call?rq=1 – Nevosis Jun 12 '17 at 12:05
  • @Nevosis [link](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call?rq=1) your link is about ajax and my question is about angularjs – user6900333 Jun 12 '17 at 12:14
  • My link and your question is about asynchronous call in javascript. It is the exact same thing in angular, react, ajax, jquery, nodejs, etc... How to handle async call in js ? Mainly promises and callbacks. – Nevosis Jun 12 '17 at 12:16

2 Answers2

1

Use callback function in $scope.is_exsis(sector); and perform this action inside callback function -

 var del_status = $scope.is_exsis(function(data){
    console.log(del_status); // is_exsis return value

    if(!del_status) {
        // save
    }
 });
Rudraksh Pathak
  • 888
  • 8
  • 20
0

You should be able to return the Promise and then chain it so that the save isn't completed until is_exists is done.

$scope.save = function(sector) {
    $scope.is_exsis(sector).then(function(del_status){

        console.log(del_status); // is_exsis return value

        if(!del_status) {
        // save
        }
   });

}

$scope.is_exist = function(sector) {
    return $http({
        method : 'POST',
        url : '/is_data_exist',
        contentType : 'application/json; charset=utf-8',
        data : angular.toJson({
            'sector': sector
        }),
        dataType: 'json',
    }).then(function successCallback(response) {

        console.log(response.data); //http respond

        if(response.data == 'False') {
            return false;
        }
        else {
            return true;
        }
    })
}
McMutton
  • 905
  • 13
  • 25
JoshSommer
  • 2,550
  • 18
  • 23