0

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.

4
  • vm.clear(val, 'Hello').then(function(value) What is vm? Commented Nov 3, 2016 at 19:59
  • Does vm.clear return a promise? Commented Nov 3, 2016 at 20:40
  • No it does not return any promise Commented Nov 4, 2016 at 16:31
  • So you cannot use vm.clear(...).then(...) and/or directly return vm.clear(...). I'll update my answer. Commented Nov 7, 2016 at 12:18

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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) {});

Comments

0

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);
                    }

                })
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.