gpt4 book ai didi

javascript - 如何使用 promise 来跳过读取拒绝访问的锁定文件?

转载 作者:行者123 更新时间:2023-12-01 02:23:05 33 4
gpt4 key购买 nike

我正在尝试制作一个 Electron-Vue 应用程序,通过调用此方法来读取目录并显示文件夹/文件:

load (path = 'C:') {
path = join(path)
this.diskLetter = path[0]
this.$store.dispatch('getContents', path).then(() => {
this.$store.commit('setPlacePath', path)
}, err => {
console.error(err)
this.$route.push('/')
})
}

问题是,当我尝试加载包含一些锁定文件的目录时,例如:

load (path = 'C:') ,它会抛出此类文件的控制台错误,例如:

Uncaught (in promise) Error: 
EPERM: operation not permitted, stat 'C:\System Volume Information'

但我希望它返回如下所示的内容(21 个中的 16 个):

(这正是 Wait until all promises complete even if some rejected 所做的)

(21) ["$Recycle.Bin", "$SysReset", "bootmgr", "Dell", "Documents and Settings", "ESD", "ffmpeg", "hiberfil.sys", "Intel", "pagefile.sys", "PerfLogs", "Program Files", "Program Files (x86)", "ProgramData", "Python36", "Recovery", "swapfile.sys", "System Volume Information", "test", "Users", "Windows"]


Error: EBUSY: resource busy or locked, stat 'C:\hiberfil.sys'
Error: EBUSY: resource busy or locked, stat 'C:\pagefile.sys'
Error: EPERM: operation not permitted, stat 'C:\Recovery'
Error: EBUSY: resource busy or locked, stat 'C:\swapfile.sys'
Error: EPERM: operation not permitted, stat 'C:\System Volume Information'

(16) [Stats, Stats, Stats, Stats, Stats, Stats, Stats, Stats, Stats, Stats, Stats, Stats, Stats, Stats, Stats, Stats]

js文件:

import fs from 'fs-extra'
import { join } from 'path'

/**
* Get Array for path contents. Checks if path exists
* @param {String} path
* @return {Promise}
*/
export function readdir (path) {
return pathExists(path)
? fs.readdir(path)
: Promise.reject(new Error('Path does not exist'))
}

/**
* Alias of fs.existsSync
* @param {String} path
* @return {Boolean}
*/
export function pathExists (path) {
return fs.existsSync(path)
}

/**
* Get path stat
* @param {String} path
* @return {Promise}
*/
export function stat (path) {
return fs.stat(path)
}

/**
* Take in file path and return contents with stat
* @param {String} path
* @return {Array}
*/
export default path => {
return new Promise((resolve, reject) => {
const contents = []

readdir(path).then(files => {
let statsPromises = []

files.forEach(file => {
statsPromises.push(stat(join(path, file)))
})

Promise.all(statsPromises).then(stats => {
stats.forEach((stat, i) => {
contents.push({
name: files[i],
path: join(path, files[i]),
stat
})
})

resolve(contents)
})
})
})
}

最佳答案

只需使用 .catch().then() 的第二个参数处理 stat 拒绝的错误:

export default function(path) {
return readdir(path).then(files => {
const statsPromises = files.map((file, i) => {
const p = join(path, file);
return stat(p).then(stat =>
({
name: file,
path: p,
stat
})
, err => null);
// ^^^^^^^^^^^^^
});
return Promise.all(statsPromises);
}).then(contents =>
contents.filter(Boolean)
);
}

关于javascript - 如何使用 promise 来跳过读取拒绝访问的锁定文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49052638/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com