NodeJS:用generic flow control来解决boomerang效应的疑惑

这段代码是在node高级编程19章Listing 19-3,cascade function 看不明白,能帮忙给解释下吗?

var fs = requirefs;
function cascadecallbacks, callback {
// clone the array var functions = callbacks.slice0; function processNexterr { if err { return callbackerr; } var args = Array.prototype.slice.callarguments; var func = functions.shift; if func { // remove first argument containing the error args.shift; } else { func = callback; } args.pushprocessNext; func.applythis, args; } processNext.callthis;
}
function append_some_a_to_bcallback { var aFd, bFd, buffer = new Buffer10; cascade[ function open_anext { fs.open__dirname + /a.txt, r, next; }, function read_from_afd, next { aFd = fd; fs.readaFd, buffer, 0, buffer.length, 0, next; }, function close_abytesRead, buf, next { fs.closeaFd, next; }, function open_bnext { fs.open__dirname + /b.txt, a, next; }, function stat_bfd, next { bFd = fd; fs.fstatbFd, next; }, function write_bbStats, next { fs.writebFd, buffer, 0, buffer.length, bStats.size, next; }, function close_bbytesWritten, buf, next { fs.closebFd, next; } ], callback;
};
console.logstarting...;
append_some_a_to_bfunction doneerr { if err { throw err; } console.logdone;
};

谢谢各位了!

cascade 函数做的, 就是把下面的每个函数, 按序依次执行, 并仅在异步任务完成后执行下一个函数, 外加错误处理.

发表评论

电子邮件地址不会被公开。 必填项已用*标注