0

So I am very new to these Promises. I am having a hard time wrapping my head around them. I have looked over plenty of tuts and SO posts, but haven't come across anything like my situation. Here is my current code.

$("#PayNow, input[name^='schedulepmt']").on("blur", function(){
 var diff = 0;
 diff = calcBalanceDue();
 if(diff){
    $.when(confirmNewPayment(diff)).then(function(){
        if($(this).attr("id") === "PayNow" && input[name^='schedulepmt'].length && ($(this).val() !== $(this).data("prevValue"))){
            if(!resetPmtInfo()) {
                $(this).val($(this).data("prevValue"));
            }
        }
    });
  }
});

I would like the code inside the .then() to execute after confirmNewPayment() has executed. It seems to be still all running async.

Thanks.

Here is confirmNewPayment()

function confirmNewPayment(diff){
bootbox.dialog({
    message: "There is an outstanding balance. How would you like to proceed?",
    title: "Outstanding Balance Options",
    closeButton: false,
    buttons: {
        addpmt: {
            label: "Add a scheduled CC payment",
            className: "btn-success",
            callback: function() {
                scheduledPmtData.PaymentCount += 1;
                $("#ScheduledPayments").append($("#ScheduledPmtTemplate").render(scheduledPmtData,{amount:diff}));
            }
        },
        collect: {
            label: "Mark balance as \"To Collect\"",
            className: "btn-primary",
            callback: function() {
                $("#BalanceDueRow").removeClass("hide");
                $("#BalanceDue").val(diff);
            }
        }
    }
  });
}
3
  • 1
    What does confirmNewPayment look like? Why are you calling $.when on it? Does it return a promise? Commented Oct 8, 2014 at 19:52
  • I added the fn. No, it doesn't return anything. Commented Oct 8, 2014 at 19:55
  • 2
    That's why this isn't working as you expect. Why do you want to use a promise here? What part of confirmNewPayment is async? When do you want the .then to run? After the dialog opens? After a button is clicked? Commented Oct 8, 2014 at 19:56

1 Answer 1

2

Your confirmNewPayment function should return a promise:

function confirmNewPayment(diff){
    var deferred = $.Deferred();

    // Your async action here
    (function() {
        // $when will run your function when the deferred object is resolved
        deferred.resolve(diff * 2);
    })();

    return deferred.promise();
}

For the update:

Everything would be easier if bootbox was returning a promise by design. So in this case you could do

function confirmNewPayment(diff){
    var deferred = $.Deferred();

    bootbox.dialog({
        message: "There is an outstanding balance. How would you like to proceed?",
        title: "Outstanding Balance Options",
        closeButton: false,
        buttons: {
            addpmt: {
                label: "Add a scheduled CC payment",
                className: "btn-success",
                callback: function() {
                    scheduledPmtData.PaymentCount += 1;
                    $("#ScheduledPayments").append($("#ScheduledPmtTemplate").render(scheduledPmtData,{amount:diff}));

                    deferred.resolve('schedule');
                }
            },
            collect: {
                label: "Mark balance as \"To Collect\"",
                className: "btn-primary",
                callback: function() {
                    $("#BalanceDueRow").removeClass("hide");
                    $("#BalanceDue").val(diff);

                    deferred.resolve('mark');
                }
            }
        }
    });

    return deferred.promise();
}

...

$("#PayNow, input[name^='schedulepmt']").on("blur", function(){
    ....
    confirmNewPayment(diff).then(function(selection) {
        // selection is 'schedule' or 'mark'
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

You don't need $.when for this. You can just do confirmNewPayment(diff).then(function(){ });
my code should go before the (function(){ or inside it?
Understanding it better now, although it is not working as intended, I think it is because I need another Promise inside resetPmtInfo() which also has a bootbox.

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.