The question is published on by Tutorial Guruji team.
I’m using the function Expo.writeAsStringAsync() (docs: https://docs.expo.io/versions/latest/sdk/filesystem/#filesystemwriteasstringasyncfileuri-contents-options). I noticed that it needs different times with different size files (as expected), but there is no way to know when it has finished, because it returns nothing. So, if I have to access the file just after writing it, I could find it empty, because maybe it’s still writing.
Is there any way to receive an answer when it has completed? like a normal promise-then-catch?
P.S.: I’ve tried to promisify it, but without success.
Answer
I can see that the API docs are misleading because they don’t specify the return type.
In fact that API call is defined as async
function (see source-code):
export async function writeAsStringAsync( fileUri: string, contents: string, options: WritingOptions = {} ): Promise<void> { // ... }
Every async
function returns a Promise (you can see in TypeScript signature above it says Promise<void>
).
This means you can use the returned Promise and await
for it or use .then()
to wait for the moment of filesystem call to complete.
await Expo.writeAsStringAsync(fileUri, contents, options); // do something when write is finished // you can catch errors with try/catch clause
or
Expo.writeAsStringAsync(fileUri, contents, options).then( () => { /* do something when write is finished */ } ).catch(err => { /* handle errors */ }