简体   繁体   中英

What is a good pattern in NodeJS for handling an error response and trying the same call again whilst avoiding callback soup?

I am looking for a good pattern for dealing with the following scenario. And, I want to know if this is something that promises would help with?

Scenario :

There is a call that returns a callback. That callback get's passed an error object. I want to examine that error object, take an appropriate action to resolve it, and make the original call again. And, I want to avoid the callback soup that my current implementation has.

(bad) Javascript Example :

doSomethingInteresting(arg0, arg1, function(err, result) {
    if(err && err.code == 1111) {
        fixTheError(err, function() {
            doSomethingInteresting(arg0, arg1, function(err, result) {
                if(err && err.code == 2222) fixTheError(err, function() {
                    // on and on for each possible error scenario...
                });
            });
        });
    }
    // do other stuff...
});

The trick is to set things up to run recursively.

function callbackFunction (err, arg0, arg1) {
    //If we pass arg0, arg1, and this along with our fix error condition, instead of 
    //making it dependent on the closure, we can make things happen recursively!
    if (err) fixAnyErrorCondition(err, arg0, arg1)
}

//Now make your fix error condition, handle any potential error.
function fixAnyErrorCondition(err, arg0, arg1) {
    if (err.condition1) {
         //fix condition1
    } else if (err.condition2) {
         //fix condition2
    }
    //Once we fix the error conditions, call the function again, using the same arguments!
    doSomethingInteresting(arg0, arg1, callbackFunction);
}

//Now just start the process!
doSomethingInteresting(arg0, arg1, callbackFunction);

Using this logic, the doSomethingINteresting function, and your error callbacks will call each other as many times as necessary, without you having to nest a potentially infinite number of callbacks. YUCH!

There is something called the Promise pattern, it is a way to emulate synchronus behavior in async.

For example, traditionally with the callback method one would write code such as this:

asyncCall(function(err, data1){
    if(err) return callback(err);       
    anotherAsyncCall(function(err2, data2){
        if(err2) return calllback(err2);
        oneMoreAsyncCall(function(err3, data3){
            if(err3) return callback(err3);
            // are we done yet?
        });
    });
});

However using the Promises pattern, one would write:

    asyncCall()
.then(function(data1){
    // do something...
    return anotherAsyncCall();
})
.then(function(data2){
    // do something...  
    return oneMoreAsyncCall();    
})
.then(function(data3){
   // the third and final async response
})
.fail(function(err) {
   // handle any error resulting from any of the above calls    
})
.done();

Both code examples were taken from i2devs . For more information just search for Promise Javascript Pattern. It is too large to include here.

Note : In terms of error handling, with the Promise pattern, if an error is thrown it is carried all the way to the .fail() method as you can see in the second example, just as you would expect in normal try/catch routines.

For Promise pattern libraries I would suggest Q or bluebird

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM