Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use FileSystem.getInfoAsync() with Expo?

I'm trying to test if a file exists with expo.

This is the expo doc : Returns If no item exists at this URI, returns { exists: false, isDirectory: false }. Else returns an object

if (this._liste.length === 0) {
               let tmp =FileSystem.getInfoAsync('file://exemple.json');

                if(tmp.exists === false){
                    alert("not found")
                }
            }

The File doesn't exist, I'm sure and the alert doesn't show. When I print tmp, I have a Object but when I print tmp.exist I have a undefined.

like image 946
Tomgalanx Avatar asked Oct 16 '25 04:10

Tomgalanx


1 Answers

getInfoAsync is an async function therefore you cannot use it like a normal function. It returns a Promise so you either need to use await or then:

await

let tmp = await FileSystem.getInfoAsync('file://exemple.json');
// use tmp.exists

then

FileSystem.getInfoAsync('file://exemple.json').then(tmp => {
// use tmp.exists
});
like image 66
Kapil Pau Avatar answered Oct 18 '25 06:10

Kapil Pau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!