비동기 패키지 문서에서 each()
메소드는 3 인수 each(coll, iteratee, callback)
을 사용합니다. 제 질문은 제 3의 인수 callback
에 관한 것이 아니라 제 2 인수 iteratee
에있는 또 다른 "콜백"함수입니다.Node.js의 Async 패키지에 대한 .each() 메소드의 콜백 함수
iteratee
은 AsyncFunction()
유형의 함수이며, 이는 또한 callback
함수를 인수로 사용합니다. 다음은이 문서에 제공된 예제입니다.
// assuming openFiles is an array of file names
async.each(openFiles, function(file, callback) {
// Perform operation on file here.
console.log('Processing file ' + file);
if(file.length > 32) {
console.log('This file name is too long');
callback('File name too long');
} else {
// Do work to process file here
console.log('File processed');
callback();
}
}, function(err) {
// if any of the file processing produced an error, err would equal that error
if(err) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
}
});
이 예에서, 두 번째 인수는
function(file, callback)
iteratee
함수이어야한다. 그러나, 나는 그것의
callback
인수가 정의 된 곳을 이해하지 못한다. 위 예제에서
callback('File name too long');
과
callback('File name too long');
으로 호출되었지만이 함수는 정확히 무엇을합니까? 직관적으로이 함수는
file
처리를 완료하면이 사실을 알릴 수 있습니다. 하지만이 코드의 정확한 코드를 어디에서 찾을 수 있습니까?
callback
?
iteratee에 함수를 전달하는'each' 구현 내에서 코드를 찾을 수 있습니다. 그러나 그것을 찾으러 가지 마십시오 - 꽤 복잡한 코드입니다. 예,'callback'은 iteratee가 끝나면 결과 나 에러로 호출되어야하고, 호출하면'async.each'가 무엇 이던지 다음 단계를 시작할 것입니다. – Bergi
실제로 비동기가 발생하지 않기 때문에이 예제는 끔찍한 것입니다. – Bergi