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
{{ message }}
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
Added the ability for the resultify function to handle synchronous code without returning a promise.
This will keep our function synchronous without having to make it asynchronous for no reason.
constmyFunction=(value: string): string=>{if(value.length<4)thrownewError("Value is too short!")// Perform operations on the value 🤷♂️returnvalue}constmySyncFunction=(value: string): string=>{constresult=resultify(()=>myFunction(value));if(!result.ok)return"hello!";returnresult.value}// For exemple, i can use my function at top-levelconsole.log(mySyncFunction("hey"));
With current version
constmyFunction=(value: string): string=>{if(value.length<4)thrownewError("Value is too short!")// Perform operations on the value 🤷♂️returnvalue}constmyAsyncFunctionButItIsSync=async(value: string): Promise<string>=>{constresult=awaitresultify(()=>myFunction(value));if(!result.ok)return"hello!";returnresult.value}// For example, I can't use my theoretically synchronous function at the top-level(async()=>{console.log(awaitmyAsyncFunctionButItIsSync("hey"));})();
Use case:
typeMyFooObject={barMethod: (callback: (value: string)=>string)=>void;};constfooObject: MyFooObject;fooObject.barMethod((value)=>{constmyOperation=resultify(()=>something(value));if(!myOperation.ok){return'a return example';}returnmyOperation.value;});// return type of `MyFooObject.barMethod` doesn't match `Promise<string>`
Possible solution:
Create a second function exactly the same, which this time would not take a MaybePromise<T> but T, and return T directly.
Added the ability for the
resultify
function to handle synchronous code without returning a promise.This will keep our function synchronous without having to make it asynchronous for no reason.
With current version
Use case:
Possible solution:
Create a second function exactly the same, which this time would not take a
MaybePromise<T>
butT
, and returnT
directly.Current:
Possible solution:
The text was updated successfully, but these errors were encountered: