A python-style with statement for deno.
Comes with someUsing types, e.g.Open,ChDir,TempDir, andTimeout.
import{using,Open}from"htps://deno.land/x/using/mod.ts";constenc=newTextEncoder();constdec=newTextDecoder();await using(newOpen("file","w"),async(f:Deno.File)=>{constdata=enc.encode("Hello world!\n");awaitf.write(data);});await using(newOpen("file","r"),async(f:Deno.File)=>{constdata=newUint8Array(20);awaitf.read(data);consttext=dec.decode(data);console.log(text);});There is also a corresponding sync versionUsingSync.
You can define your ownUsing types by creating a class which implementsUsing:
exportclassOpenimplementsUsing<Deno.File>{constructor(filename:string,mode?:Deno.OpenMode){this.filename=filename;this.mode=mode;}publicasync_aenter(){this.file=awaitDeno.open(this.filename,this.mode);returnthis.file;}publicasync_aexit(e){this.file.close();}privatefile:Deno.File;privatefilename:string;privatemode:Deno.OpenMode;}