不要使用
forEach
具有
async
它们不能一起工作,你不能等待循环。使用标准
for .. of
循环,与
async/await
const readDir = async (dir) => {
const items = await FileSystem.readDirectoryAsync(dir);
for (let item of items) {
const f = await FileSystem.getInfoAsync(item);
if (f.isDirectory === true) {
await readDir(f.uri);
} else {
console.log("f.uri: ", f.uri);
fileList.push(f.uri);
}
}
}
readDir
是
异步
当然,你也必须
await
then
async function foo() {
const parentDirectory = "parent_folder";
await readDir(parentDirectory);
//do some other stuff once readdir is finished.
}
或
const parentDirectory = "parent_folder";
readDir(parentDirectory).then(_ => {
//do some other stuff once readdir is finished.
}