TransWikia.com

Не помогает try cath при использовании модуля fs в node. Возникает ошибка - Error: EBUSY: resource busy or locked, lstat

Stack Overflow на русском Asked on December 7, 2021

Почему не помогает try cath в этом случае? Есть варианты как быть?
Вот такая ошибка возникает:

[Error: EBUSY: resource busy or locked, lstat 'C:hiberfil.sys'] {
  errno: -4082,
  code: 'EBUSY',
  syscall: 'lstat',
  path: 'C:\hiberfil.sys'
}

Вот такой код:

async function getStatusDirectory() {

  try {
    const path = 'C:/'
    const dirIterable = await fs.promises.opendir(path);
    let dir = []
    for await (const dirent of dirIterable) {
      dir.push({ ...dirent });
    }
    console.log(dir)
    let promises = await Promise.all(dir.map(dir => {
      let promise
      try {
        promise = fs.promises.lstat(path + dir.name)
      } catch (error) {
        console.log(error)
        promise = { locked: true }
      } finally {
        return promise
      }
    }))
    console.log(promises)
  } catch (error) {
    console.log(error)
  }

}
getStatusDirectory()

скриншот консоли

One Answer

Нужно использовать Promise.allSettled()

async function getStatusDirectory() {

  try {
    const path = 'C:/'
    const dirIterable = await fs.promises.opendir(path);
    let dir = []
    for await (const dirent of dirIterable) {
      dir.push({ ...dirent });
    }
    console.log(dir)
    let promises = await Promise.allSettled(dir.map(dir => {
      return fs.promises.lstat(path + dir.name)
    }))
    console.log(promises)
  } catch (error) {
    console.log(error)
  }

}
getStatusDirectory()

Answered by Роман Василаки on December 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP