Reputation: 1097
I am trying to call the function itself within the function.
Basically it will call the function itself to make another request if the first request has the id.
I have something like
function test(variable) {
var q = $q.defer();
$http({
method: 'get',
url: 'myurl.com'
}).then(function(returnData) {
if(returnData.getNewInfo) {
test(returnData.id).then(function(secondData){
q.resolve(secondData);
})
} else {
q.resolve(returnData)
}
});
}
return q.promise;
}
I am getting
test(...).then
is not a function error in the console on this line
test(returnData.id).then(function(secondData){
I am trying to call the function itself inside the promise. Not sure how to fix my issue. Can anyone help me about it? Thanks a lot!
Upvotes: 2
Views: 88
Reputation: 3467
You can return your $http
call. Promises are chainable and $http
returns a promise.
function test(variable) {
return $http({
method: 'get',
url: 'myurl.com'
}).then(function (returnData) {
if (returnData.getNewInfo) {
return test(returnData.id);
} else {
return returnData;
}
});
}
Upvotes: 1
Reputation: 5873
Fix your code indentation and you'll get the below - see my comment about the stray brace. So the function is returning undefined rather than q.promise
.
function test(variable) {
var q = $q.defer();
$http({
method: 'get',
url: 'myurl.com'
}).then(function(returnData) {
if (returnData.getNewInfo) {
test(returnData.id).then(function(secondData) {
q.resolve(secondData);
})
} else {
q.resolve(returnData)
}
});
} // <------- end of function - should be removed?
return q.promise;
}
Upvotes: 1
Reputation: 1703
Try this:
function test(variable, callback) {
/*snip*/
callback();
/*snip*/
}
test("foo",test);
Upvotes: 0