0

I have these methods

function successHandler(result/*,deferred*/) {
    var strResult = "";
    if (typeof result === 'object') {
        strResult = JSON.stringify(result);
    } else {
        strResult = result;
    }
    var mes = "SUCCESS: \r\n" + strResult;
    NotificationService.alert(mes, "پيغام", "تاييد");
    //deferred.resolve();
}

function errorHandler(error/*,deferred*/) {
    var mes = "ERROR: \r\n" + error;
    NotificationService.alert(mes, "خطا", "تاييد");
    //deferred.reject();
}

function init() {
    //var deferred = $q.defer();
    inappbilling.init(successHandler, errorHandler, { showLog: true });
    //return deferred.promise;
}

I need to create a deffer object and pass it to the success and error handler (like what I commented), since the callbacks have another argument by default I am really confused how to do that

2 Answers 2

1

You can wrap your handlers in an anonymous function, and capture the reference to deferred in its closure, like this:

inappbilling.init(
    successHandler,
    function(error){ errorHandler(error,deferred); },
    { showLog: true }
);

I'm not sure whether this is a good pattern you're trying to implement, but the above method should achieve what you're looking for.

A benefit of this is that the interface towards your error handler doesnt change (in the above case, only one param, the error object), but internally you're using your multi-param handler.

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

Comments

1
inappbilling.init(
  function(result) { successHandler(result, deferred) },
  function(error) { errorHandler(error, deferred) },
  { showLog: true }
);

Your deferred will be captured in the little closure, and your handlers will be called with the proper arguments.

1 Comment

Thanks it works, since @doldt answer is posted sooner I accept his answer

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.