0

I'm trying to call a promise function from it's own response in this kind of pattern:

f().then(function(response){
    if(response){
        f(response).then(function(response){
            if(response){
                f(response).then.....
            }
        });
    }
});

I'm working with angularjs. Thanks.

P.S.:

This examples are working but the third:

FIRST EXAMPLE: working:

var a = ['one','two','four'];

angular.forEach(a, function(number){    
    f(number).then(function(response){
        console.log(response);
    }); 
});

SECOND EXAMPLE: working:

var a = ['one','two','four'];

angular.forEach(a, function(number){
    f(number).then(function(response){
        if(response){
            f(response).then(function(response2){
                if(response2){
                    f(response2).then(function(response3){
                        console.log(response3); //this is the behaviour I'm trying to automate
                    });
                }
            });
        }
    });
});

THIRD EXAMPLE: not working:

var a = ['one','two','four'];

angular.forEach(a, function(number){

    var t = '';

    f(number).then(function(response){
        t = response;
        while(t != ''){
            f(t).then(function(response){
                console.log(response);
                t = response;
            });
        }
    });
});

At the end I did so:

var a=['one','two','four'];
var simple = function(token){
getasync(a[a.length-1], token).then(function(resptoken){
    console.log(resptoken);
    if(resptoken) 
        simple(resptoken);
    else if (a.length>1){ 
         a.pop() ;
         simple(resptoken);
    }else{
         console.log("theend");
    }
});
}
J. Doe
  • 1
  • 4

2 Answers2

0

You should do something like

function call (response){
   f(response).then(function(response){
      if(response){
         call(response);
      }
   });
}

call(response);

if promise was resolved, it will again call itself

Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
0

You are asking how to create a promise where the final resolution depends on one or more fetches from an asynchronous service. If fact you don't know what or how many subsequent fetches you need until you examine data from previous fetches.

This is a job for the much maligned $q.defer(). Before you ask Is this a “Deferred Antipattern”?, please bear with me for a moment and examine my example.

In my example, I am going to compute N factorial, where the application sends N to a server for advice on what to do next. (Compute N-1 factorial). The application keeps asking until it receives advice to compute 1 factorial which it knows.

First step is to create a $q.defer and use the promise from it to display the answer.

//Function to compute N! asynchronously
//
vm.asyncBangCompute = function(n) {

    var bangDefer = $q.defer();
    var bangPromise = bangDefer.promise;

    //Answer returned here
    //
    bangPromise.then ( function (answer) {
      vm.nAnswer = answer;
    }) .catch (function (error) {
      console.error(error);
    });

    vm.nBangSoFar = [];
    vm.nCompute = n;
    vm.nAnswer = "PENDING";

    asyncBangResolve(bangDefer, n, 1);
  };

Next the resolver function recursively calls the pseudo HTTP service and itself until it resolves an answer.

//Recursively call the pseudoHttpService
//
function asyncBangResolve(defer, n, soFar) {
    if (n==1) {
      defer.resolve(soFar);
    } else {
      //push partial results
      vm.nBangSoFar.push(" "+(n*soFar)+" = "+n+"*"+soFar);
      //
      var nextPromise = pseudoHttpService(n,soFar);
      nextPromise.then (function (result) {
        asyncBangResolve(defer,result.data.n,result.data.soFar);
      });
    }
  }

The DEMO on Plunker.

Community
  • 1
  • 1
georgeawg
  • 48,608
  • 13
  • 72
  • 95