javascript - 由于回调、异步和等待而面临错误
问题描述
我需要使用回调来获取目录中的文件并使用我的函数处理它们
async function getFilesFromDirectory(path, callback) {
await fs.readdir(path, function (err, content) {
if (err) return callback(err);
callback(null, content);
});
}
checkVideo = async (filePath) => {
let folderpath = path.join(__dirname, "../videoScreenshots");
let result = await getFilesFromDirectory(folderpath, async (err, files) => {
let finalVal = [];
finalVal = await filereader(files);
console.log("final checked vals :", finalVal);
return finalVal;
});
console.log("result:", result);
};
但我不知道为什么,异步等待似乎没有在回调上工作,我需要将最终检查的 vals(稍后打印)返回到变量结果中,以便我可以将其进一步返回到另一个函数中。我已经尝试了很长一段时间,但它似乎不起作用.. 请帮助我.. :(
第一行..结果:未定义
[0] result: undefined
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-12.185599999999999-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-12.185599999999999-seconds.png
[0] node-pre-gyp info This Node instance does not support builds for N-API version 7
[0] node-pre-gyp info This Node instance does not support builds for N-API version 8
[0] node-pre-gyp info This Node instance does not support builds for N-API version 7
[0] node-pre-gyp info This Node instance does not support builds for N-API version 8
[0] 2021-06-02 22:49:41.566584: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2
[0] To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-18.278399999999998-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-18.278399999999998-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-24.371199999999998-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-24.371199999999998-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-30.159359999999996-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-30.159359999999996-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-6.0927999999999995-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-6.0927999999999995-seconds.png
[0] { violence: 78.71493101119995, nudity: 60.231149196624756 }
[0] { violence: 98.11700582504272, nudity: 45.4850971698761 }
[0] { violence: 97.38714694976807, nudity: 87.78105974197388 }
[0] { violence: 98.76883029937744, nudity: 8.434739708900452 }
[0] { violence: 76.18809938430786, nudity: 61.19433045387268 }
[0] final checked vals : [
[0] { violence: 78.71493101119995, nudity: 60.231149196624756 },
[0] { violence: 98.11700582504272, nudity: 45.4850971698761 },
[0] { violence: 97.38714694976807, nudity: 87.78105974197388 },
[0] { violence: 98.76883029937744, nudity: 8.434739708900452 },
[0] { violence: 76.18809938430786, nudity: 61.19433045387268 }
[0] ]
解决方案
推荐阅读
- c - 节点 mcu 不显示 RGB 值
- amazon-web-services - 从应用程序向 Cloudwatch 代理发送自定义指标数据
- php - 无法调试位于网络驱动器上的 .php 文件
- python - 使用 numpy.nditer 访问不同的单元格
- datastax - cassandra 快照不适用于用户定义的冻结类型列表
- csv - 如何在 sas 存储过程中生成多个 CSV 文件?
- c# - 在 Delphi VCL 框架上显示 C# WinForms 面板
- python - 我想抓取这些产品信息
- c# - 无法运行使用 .net 6.0 构建的本机 iOS 应用程序:缺少 libhostpolicy.dylib
- java - 使用一维数组打印最长公共子序列 (LCS) 元素