const toBool = [() => true, () => false]
The above line is used in this MDN guide to (seemingly) evaluate the output from Node’s fs.promises.access
method as a boolean.
The snippet in question is below, paraphrased from the MDN example:
const toBool = [() => true, () => false];
const prepareFile = async (url) => {
...
const exists = await fs.promises.access(filePath).then(...toBool);
...
};
The code works as intended; I’m hoping for an explanation as to how the compiler evaluates these lines, as I can’t quite wrap my head around it.
As best as I can tell, when fs.promises.access
returns undefined
(a successful resolution, according to the Node docs for the access method), exists
is set to true
, while the return of an Error
object sets exists
to false.
Can someone explain how this line evaluates to arrive at true
for exists
in the snippet above?