I have an existing function that returns an AJAX promise. I want to update this function to display a confirmation alert before running the AJAX call, but other parts of the code use this function and is already expecting a promise.
Here's what my original function looks like:
function doTheDeed() {
return $.ajax({
url: '/api/delete/123',
method: 'POST'
}).done(function () {
alert('The deed is done.');
});
}
doTheDeed().done(function {} { /*Do something else*/ });
Now I want to confirm with the user before running the AJAX call. How do maintain the API agreement of returning a promise, while waiting for the users confirmation or cancel?
function doTheDeed() {
bootbox.confirm('Are you sure?', function (result) {
if (result) {
// Too late to return a promise, promise should've been returned a long time ago
return $.ajax({
url: '/api/delete/123',
method: 'POST'
}).done(function () {
alert('The deed is done.');
});
} else {
//return what??
}
});
}
doTheDeed().done(function {} { /*Do something else*/ });
Do I need to conditionally return different promises based on the user's response?