user3147957
user3147957

Reputation: 1

How to stop execution of next Statements if an Async function returns any errors?

I have below couple of statements with in a javascript function.

postTransaction function is having a async call, which is calling a webservice and returns success or failure. I have two call back methods for success(transactionSuccess) and failure(transactionFailure).

Here my problem is if the service got failure i should stop executing the next statement i.e return requestDetails(); I dont want to use setTimeOut function here. Any other ways to handle my situation?

function doSomeThing () {

    postTransaction(objSelectedAccount,transactionSuccess,transactionFailure); 

    return requestDetails();   
}

function requestDetails () {

    return true;
}

function postTransaction () {

    $.ajax('URL', {
        method: "POST",
        dataType: "json",       
        data: {}, 
        success: function (payload) {
            callBackSucces(payload); 
        }, 
        error: function(xhr, statusText, ex) {           
            callBackFailure(xhr);
        }
    });
}

Upvotes: 0

Views: 1481

Answers (4)

6502
6502

Reputation: 114481

There is no way to change an asynchronous operation into a synchronous operation (i.e. to "wait" for the outcome and then call or not something else).

You must rework and break down your logic into smaller parts so that the operation are done (or just started) in the "success" callback.

Note that many asynchronous operations are indeed just requests to a server and it's possible to make a synchronous request (i.e. performing a "get" blocking while wainting for the answer instead of using callbacks) but this is considered not a good idea in general because the application becomes less responsive.

Unfortunately (and for reasons I don't fully understand) Javascript doesn't expose the event dispatching primitive and thus is impossible to use what is normally done in other event-driven environments like GUIs when asynchronous is very inconventient (e.g. requesting of a confirmation) that is a "nested event loop".

The fact that you cannot build a synchronous operation from an asynchronous one is also the reason for which in node for many functions there are the equivalent Synch versions that would be impossible for the application programmer to create.

Upvotes: 0

Siva Tumma
Siva Tumma

Reputation: 1701

function doSomeThing () {
    postTransaction(objSelectedAccount,transactionSuccess,transactionFailure); 
}

function requestDetails () {
    return true;
}

function postTransaction () {

    $.ajax('URL', {
        method: "POST",
        dataType: "json",       
        data: {}, 
        success: function (payload) {
            requestDetails(payload); 
        }, 
        error: function(xhr, statusText, ex) {           
            callBackFailure(xhr);
        }
    });
}

OR,

use synchronous request.

Upvotes: 0

Shin
Shin

Reputation: 189

If requestDetails() may not be called until the transactionSuccess was called, then you may put the requestDetails into the transactionSuccess. And your function doSomehting should not be a synchronous function.

So, change your code to something like this:

function doSomething(callback) {
  postTransaction(objSelectedAccount, function () {
    // success and do something..
    callback requestDetails();
  }, transactionFailure); 
}

A function, which have a asynchronous step, should also be a asynchronous function.

I hope this could help you, and please forgive my poor English..

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

You can't stop the next statement from executing based on the result of an async operation. That's the whole point of async operations! You also don't want to make the ajax request a blocking request, because that will freeze the UI until the request completes -- a bad user experience that might also lead to a "script has stopped responding" alert from the browser.

The best you can do is move whatever processing you want delayed into the success handler for the ajax call. Don't think of the ajax call as something that "returns success or failure"; it doesn't actually return anything (in the JavaScript sense). Instead, think of it as something that generates either a success or a failure event at some future time and code your app to handle the events.

Upvotes: 1

Related Questions