Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 理解回调错误参数需要async.js解释_Javascript_Async.js - Fatal编程技术网

Javascript 理解回调错误参数需要async.js解释

Javascript 理解回调错误参数需要async.js解释,javascript,async.js,Javascript,Async.js,这可能是一个简单的问题,但我写信是想问一个问题,因为我根本不明白。参数“null”在async.com中的用途是什么?下面的一些示例?根据文档,参数应该会出错,但是在回调中传递错误又有什么意义呢 async.some(['file1','file2','file3'], function(filePath, callback) { fs.access(filePath, function(err) { callback(null, !err) }); }, function(err, r

这可能是一个简单的问题,但我写信是想问一个问题,因为我根本不明白。参数“null”在async.com中的用途是什么?下面的一些示例?根据文档,参数应该会出错,但是在回调中传递错误又有什么意义呢

async.some(['file1','file2','file3'], function(filePath, callback) {
fs.access(filePath, function(err) {
    callback(null, !err)
});
}, function(err, result) {
    // if result is true then at least one of the files exists
});
我做了一些实验,因为我不知道error参数是如何到达主回调错误参数的

callback('err', true) // main callback returns 'err' and undefined. 
                      // second argument 'true' got lost?

callback(true) // main callback returns true and undefined.
               // did not pass error argument but still works without the first argument?

区分流程(您的某个任务)遇到错误时的错误和成功时的错误非常有用。当部分完成后,您可能想知道结果以及是否发生错误,并分别处理这些情况。 就异步js而言,任何作为错误传递的错误值都将被视为非错误;如果任何文件发生错误,则只会将错误传递给回调

在您提供的代码示例中

callback('err', true) // An error is passed so true will not be passed to final callback

callback(true) // true is the error, as an error is passed, only true (the error) and no result will be passed to the final callback.

本质上,任何作为第一个参数传递给回调函数的值都会导致立即错误

区分进程(您的某个任务)遇到错误时的错误和成功时的错误非常有用。当部分完成后,您可能想知道结果以及是否发生错误,并分别处理这些情况。 就异步js而言,任何作为错误传递的错误值都将被视为非错误;如果任何文件发生错误,则只会将错误传递给回调

在您提供的代码示例中

callback('err', true) // An error is passed so true will not be passed to final callback

callback(true) // true is the error, as an error is passed, only true (the error) and no result will be passed to the final callback.

本质上,作为回调函数的第一个参数传递的任何值都将导致立即错误

表示,结果将是数组中通过真值测试(iteratee)的第一项,或者如果没有通过,则为未定义的值。使用(err,result)调用。很抱歉,当我实际询问async.some时,我错误地引用了async.detect。表示,结果将是数组中通过真值测试(iteratee)的第一项,或者如果没有通过,则为未定义的值。使用(err,result)调用。很抱歉,当我实际询问async.some时,我错误地引用了async.detect。