如何使用事件参数调用函数
我使用下面的代码正在工作:
yauzl.open(filePath, function (err, zipFile) { zipFile.on('entry', (entry) =>{ //console.log(entry.fileName); if (/\/$/.test(entry.fileName)) { return; } zipFile.openReadStream(entry, (err, readStream) => { if (err) { logger.info(err); reject(err); } else { ...
现在我想更改一下代码,使其更具可读性,如下所示:
yauzl.open(filePath, (err, zipFile) => { //zipFile = zipFile; if (err) { __rejectAndLog(err); return; } zipFile.on('entry', __processEntry(zipFile)) .once('error', __rejectAndLog) .once('close', () => { console.log(`Unpacked ${numOfFiles} files`); resolve(); }); });
这是processEntry:
function __processEntry(zipFile) { zipFile.openReadStream(entry, (err, readStream) => { if (err) { __rejectAndLog(err); return; } if (/\/$/.test(entry.fileName)) { // directory file names end with '/' return; }
在这里我得到的错误cannot use **openReadStream** of undefined
。 我该如何处理?
zipFile.openReadStream(entry, (err, readStream) => {
另外我还需要通过入口value
任何想法?
使用bind zipFile.on('entry', __processEntry.bind(null, zipFile))
并重新声明你的__processEntry作为function __processEntry(zipFile, entry)