Reputation: 839
I am not sure what i am doing wrong here -
function clearBefore(val) {
if (val.isEnrolled && val.name === 'XYZ') {
vm.clear(val, 'Hello').then(function(value){
return value;
});
return true;
}
}
then calling it -
clearBefore(val).then(function (returnVal) {});
I am new to promises, please let me know how the function should be updated to return a promise.
Upvotes: 0
Views: 63
Reputation: 1154
Inside the "if" you are returning "true" and outside the "if" you are returning nothing so you cannot use the return of clearBefore() as a promise.
Maybe this is what you want:
function clearBefore(val) {
if (val.isEnrolled && val.name === 'XYZ') {
return $q.resolve(vm.clear(val, 'Hello'));
}
return $q.reject();
}
clearBefore(val).then(
function(val) {
console.log('ok', val);
},
function() {
console.log('not ok...');
}
);
Now every return point of the function returns a promise. We have a $q.resolve()
inside the "if" and a $q.reject()
outside to return a failed promise.
Upvotes: 1
Reputation: 839
To resolve the issue i did something like following (introduced promise to the function)
function clearOTPBefore(page) {
return $q(function (resolve, reject) {
if (val.isEnrolled && val.name === 'XYZ') {
return vm.clear(val, 'Hello').then(function(value){
return resolve(value);
});
} else {
resolve(true);
}
})
}
Upvotes: 0
Reputation: 1802
It seems that you don't return anything at the end of the clearBefore() function, so try to do so:
function clearBefore(val) {
if (val.isEnrolled && val.name === 'XYZ') {
vm.clear(val, 'Hello').then(function(value){
return value;
});
}
return val; // here you should return what you need
}
and after that this:
clearBefore(val).then(function (returnVal) {});
Upvotes: 0
Reputation: 854
You need to return your promise
function clearBefore(val){ if (val.isEnrolled && val.name === 'XYZ') { return vm.clear(val, 'Hello').then(function(value) { return value; }); return true; } }
something like this
Upvotes: 0