You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Syntactic sugar for asynchronous functions, promises, generators and synchronous functions.
π¬ Usage
import{ofAnyCase}from"@await-of/of";constpromise=()=>newPromise((resolve,_reject)=>{resolve({data: true});});constconfig={defaults: "π€· Default value in case of error",error: newError("π Custom error, replaces thrown error"),retries: 3,// π Third time's a charmtimeout: 1_000,// β±οΈ Delay before timeout error};// no error thrownconst[result,error]=awaitofAnyCase(promise(),config);console.log(result);// { data: true }console.warn(error);// no error thrown, so it's undefined
ΚβΟβΚ Compare to GoLang style
import{of}from"@await-of/of";interfaceDatabaseConnectionInterfaceextendsAsyncDisposable{isConnected: boolean;version: string;}asyncfunctiongetDatabaseConnection(): Promise<DatabaseConnectionInterface>{if(Math.random()>0.5){return{isConnected: true,version: "18.3",[Symbol.asyncDispose]: async(): Promise<void>=>{console.log("Closing connectionβ¦");},};}thrownewError("No connection available");}asyncfunctionjsStyleErrorHandling(): Promise<void>{try{await using connection=awaitgetDatabaseConnection();console.log("Connection established successfully using JS-style.");console.dir(connection);}catch(error){console.warn("Something went wrong in our JS-style example");console.warn(`${error}`);return;}}asyncfunctiongoStyleErrorHandling(): Promise<void>{const[connection,error]=awaitof(getDatabaseConnection());if(error){console.warn("Something went wrong in our GoLang-style example");console.warn(`${error}`);return;}console.log("Connection established successfully using GoLang-style.");console.dir(connection);}awaitPromise.all([jsStyleErrorHandling(),goStyleErrorHandling()]);