What are the benefits of using the error?

Programmers seem to be divided on how to receive error notification asynchronously.

Some programmers prefer to use a callback with two arguments: a value and a boolean value that indicates whether this value is erroneous. This has the advantage of looking like an operator try catch:

asyncFunct(function (value, noError) {
    if (noError) {
        // success, do something with value
    } else {
        // value is the error which is thrown
    }
});

Others prefer negative (i.e. a boolean should indicate whether the value is erroneous). Their argument is that if you know that an asynchronous function will never cause an error, you can safely omit the second parameter as follows:

asyncFunction(function (value, isErroneous) {
    if (!isErrorneous) {
        // success, do something with value
    } else {
        // value is the error which is thrown
    }
});

asyncFunction(function (value) {
    // success, do something with value
});

, . , , errbacks, none:

asyncFunction(function (value) {
    // success, do something with value
}, function (error) {
    // handle the error
});

asyncFunction(function (value) {
    // success, do something with value
});

asyncFunction(null, function (error) {
    // handle the error
});

, . , , , .

+5
1

:

, . , " " ( - - - - ).

:

(filesystem.fileRead FILE_DONT_EXISTS, FILE_LOCKED, NOT_PERMISSIONS..), (db.checkConnection db.openConnection).

:

API Amazon, . http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html

: asynch, copyObject(params = {}, callback), , 2 : err (Error) data (Object) function(err, data) { ... }. , , , . .

// request

getObject({
   param1 : something,
   param2 : something,
   param3 : something
}, callback);

// response

function callback(error, response){
   if error throw err;
   // now deal with responsei
}

, . , ( ).

+1

All Articles