bdsqqq / try Goto Github PK
View Code? Open in Web Editor NEWDon't let the Try Catch Tower of Terror destroy your beautiful one liners.
Home Page: https://trytm.vercel.app
License: MIT License
Don't let the Try Catch Tower of Terror destroy your beautiful one liners.
Home Page: https://trytm.vercel.app
License: MIT License
I'm glad someone released a library like this, I've been using this pattern for quite some time now, it rocks!
This issue is only a suggestion for what could be, perhaps, an alternative export with a different return signature
import { $try } from '@bdsqqq/try'
const promise = () =>
new Promise((res) =>
setTimeout(() => res('aha!'), 1000)
)
const promiseThatRejects = () =>
new Promise((_, rej) =>
setTimeout(() => rej(new Error('oh no')), 1000)
)
const result = await $try(promise())
if (!result.error) console.log(result.data)
const otherResult = await $try(promiseThatRejects())
if (!otherResult.error) console.log(otherResult.data)
This allows for better naming through not repeating yourself, sometimes
const fetched = await $try(fetchItems())
if (fetched.error) return handleError(fetched.error)
const transformed = await $try(transform(fetched.data))
if (transformed.error) return handleError(transformed.error)
Is less repetitive to write than
const [fetched, fetchedError] = await $try(fetchItems())
if (fetchedError) return handleError(fetchedError)
const [transformed, transformedError] = await $try(transform(fetched.data))
if (transformedError) return handleError(transformedError)
There is an argument about semantics and line width here as well, but I'm way too hungover to remember
after handling the error and we are sure that the data is not null anymore, typescript cannot detect this and considers the data nullable. ts is still thinking about [null, error]
I was curious if it could be fixed though it is a typescript limit
const fetchData = () => Promise.resolve({id: 1})
const [data, error] = await trytm(fetchData())
if (error) {
return
}
data.id // data is possibly null
The License file is not being picked up by GitHub or NPM due to the misspelling of the file in the root of the repo.
Hi, first thanks for making this!
I just tried it but it seems the types that are transpiled in the packags look like this:
declare const trytm: <T>(
promise: Promise<T>,
) => Promise<readonly [Awaited<T>, null] | readonly [null, unknown]>;
export { trytm };
And the part that breaks is the | readonly [null, unknown]>
Because now if I try to use the package
const [data, error] = await trytm(myPromise())
error
is unknown
so even if I check if error !== null
afterwards, the data type is not narrowed to not be null
.
When copy/pasting the source in a helper directly in project it works alright.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.