0

I have written the code

// Handlers
function successHandlerFactory (savedFlag) {

  return function (res, savedFlag){
    if (res.data && res.status == 200) {
      ngcoupon_offerManager.addOffers(res.data.offers, -1, savedFlag);
      console.log('offers response', res, 'savedFlag', savedFlag);
    } else {
      console.error('something is wrong to get offers', res);
    }
  }
};

var offerSuccessHandler = function() {
    return successHandlerFactory();
}();
var savedofferSuccessHandler = function () {
  return successHandlerFactory(true);
}();

but apparently its giving out savedFlag undefined every executinon I make.

How come this does not work

user2167582
  • 5,986
  • 13
  • 64
  • 121

1 Answers1

5

The issue is in this part of the code:

function successHandlerFactory (savedFlag) {
  return function (res, savedFlag){
    ...

You're re-declaring savedFlag in the inner function, which ends up being the variable that is captured in the success handler. Try simply removing the second parameter of the returned function.

Patrik Oldsberg
  • 1,540
  • 12
  • 14