deploy_feedback icon indicating copy to clipboard operation
deploy_feedback copied to clipboard

Missing Deno.readTextFileSync

Open pedropaulosuzuki opened this issue 3 years ago • 4 comments

Hey! How's everyone going? I hope everyone's doing well.

I tried using Deno.readTextFileSync and I realized it is still unsuported. Of couse one can make it work by replacing it with this:

const file = Deno.readTextFileSync('./path').trim(); // Best method
const file_2 = ( await Deno.readTextFile('./path') ).trim(); // A way around it

It works, but can become a bit messy. Are there any plans to implement this and other missing Deno APIs? These can get a bit messy if we need to perform them under "non async" functions. Thanks!!

pedropaulosuzuki avatar Oct 26 '21 16:10 pedropaulosuzuki

No, we are not planning to introduce the sync APIs. Sync APIs block all execution, which can cause other requests to hang. If you are reading a large file with a sync API for example, setTimeout will not fire while that file is being loaded.

You should always use asynchronous APIs.

lucacasonato avatar Oct 26 '21 16:10 lucacasonato

No, we are not planning to introduce the sync APIs. Sync APIs block all execution, which can cause other requests to hang. If you are reading a large file with a sync API for example, setTimeout will not fire while that file is being loaded.

You should always use asynchronous APIs.

In my case, I use some server-side rendering which currently reads some pretty small files only once during setup, which I might optimize later, but the behavior I need is a sync one, which would be non-blocking anyway due to it being performed once before any requests are processed. And one can always do ( await Deno.readTextFile('./path') ) anyway

pedropaulosuzuki avatar Oct 26 '21 21:10 pedropaulosuzuki

I cant see how there is ever a technical reason for using sync instead of async.

I my mind you just put an "await" in front of the sync one and you have the same behaviour. There must be something I dont understand about your setup @pedropaulosuzuki

mathiasrw avatar Oct 27 '21 07:10 mathiasrw

I cant see how there is ever a technical reason for using sync instead of async.

I my mind you just put an "await" in front of the sync one and you have the same behaviour. There must be something I dont understand about your setup @pedropaulosuzuki

Well, you cannot use await outside of async functions unless you're at the global scope. And when you start chaining methods, it only works with the ugly parenthesis around it.

function foo() {
     // Won't work inside this function unless it is async
     const data = ( await Deno.readTextFile('./path.csv') ).trim().split('\n')
         .map(row => row.trim().split(';').map(cell => cell.trim()));
         
     // This would
     const data = Deno.readTextFileSync('./path.csv').trim().split('\n')
         .map(row => row.trim().split(';').map(cell => cell.trim()));
}

pedropaulosuzuki avatar Oct 27 '21 12:10 pedropaulosuzuki

Deno.readTextFileSync is now supported on Deno Deploy.

lucacasonato avatar Jul 05 '23 14:07 lucacasonato