How Promises works in Angularjs

Asked

Viewed 1,714 times

2

I’m having a little trouble understanding the workings of Promises of Angularjs. I have the following code:

function validateUser(name, pw) {

  var status = '';

  var data = {login: name, senha: pw, plataforma: 'APP'};

  $http.post('http://localhost:8100/login', data)
    .then(function(response) {

      console.log(response.data.retorno);

      var status = response.data.retorno;

    }, function(err) {
      console.log(err)
  });
}

function teste(name, pw) {

  var status = validateUser(nome, pw);
  alert(status);
  ...
}

The code works perfectly. However I cannot return the answer of a $http.post for another function. I can only get the answer of that precedent in .then(function(){ ... }

In jQuery we used the async: false to solve this problem. There is something similar in Angularjs?

  • You could put this return value into a controller variable or pass it as a parameter to another function. A callback, for example.

  • It’s not a "problem", that’s how it works : ) Using synchronous requests (async: false in jquery) not recommended.

  • but how would I make my test Function() wait for return? I tried to follow the angular documentation and use var deferred = $q. Defer(); but I was not successful!

  • What you want to do (really) with the data, other than Alert?

  • @bfavaretto: I have the validateUser() function in my services.js file and have the test() function inside my controller.js. I want to receive this feedback in my controller to redirect the user to some page (depending on the msm status). I think it would be nice to do this redirection within services.js

  • As I am starting to work with Angularjs now. Perhaps my logic is not correct. I’m not sure if I should use http.post inside services. Or change the logic completely and use it inside controller.js

  • It is recommended to keep the controller as clean as possible. This request logic can stay in the service and only receive controller calls to be handled in the view. I recommend reading the styleguide adopted by the community: https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md

Show 2 more comments

1 answer

3


You could try to do something like:

function validateUser(name, pw, callback) {

    var status = '';

    var data = {login: name, senha: pw, plataforma: 'APP'};
    $http.post('http://localhost:8100/login', data)
        .then(
            function successCallback(response) {
                callback(response.data);
            },
            function errorCallback(response){
                callback(response);
            }
        );
}

function teste(name, pw) {
    var status = validateUser(nome, pw, function(result){
        console.log(result);
    });
    alert(status);
    ...
}
  • Thanks @Hamurabi Araujo. That’s exactly what I couldn’t do! :)

  • I am happy to have helped. Any doubt I am available. I went through a very similar problem a few days ago ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.