nh-utils
v0.1.5
Published
This project contains the most used code tools in [nata.house](https://natahouse.com/), it's a tree-shakeable lib.
Downloads
154
Readme
This project contains the most used code tools in nata.house, it's a tree-shakeable lib.
Functions
to
To avoid problems with encapsulation and to make the code clean, is not good to use try catch statements, for this problem nata.house created a method called to
.
So this statement
let result: Result | undefined = undefined;
try {
result = await someMethodThatReturnsPromise();
} catch (error: Error) {
throw error;
}
Become this
const [error, result] = await to(someMethodThatReturnsPromise());
if (error) {
throw error;
}
toRequest
This function works almost like the to
function, but is made for apisauce results.
It checks if the result has a problem and it's status is not ok, if this condition matches, it's an error.
if (data.problem && !data.ok) {
throw data;
}
return [undefined, data];