open-draft / until Goto Github PK
View Code? Open in Web Editor NEWGracefully handle Promises using async/await without try/catch.
License: MIT License
Gracefully handle Promises using async/await without try/catch.
License: MIT License
I am curious why this change was introduced.
// Original implementation
export const until = async <DataType = unknown, ErrorType = Error>(promise: () => Promise<DataType>): Promise<[ErrorType, DataType]> => {
try {
const data = await promise()
return [null, data]
} catch (error) {
return [error, null]
}
// Current implementation
export const until = async <DataType = unknown, ErrorType = Error>(promise: () => Promise<DataType>): Promise<[ErrorType, DataType]> => {
try {
const data = await promise().catch((error) => { // .catch added
throw error
})
return [null, data]
} catch (error) {
return [error, null]
}
Is there a benefit of calling .catch
on the promise? It is my understanding that the catch block would handle throws?
Hi @kettanaito! ๐ Hope you are well :)
The return type doesn't agree with the null
values returned here:
export const until = async <DataType = unknown, ErrorType = Error>(promise: () => Promise<DataType>): Promise<[ErrorType, DataType]> => {
try {
const data = await promise().catch((error) => {
throw error
})
return [null, data] // ๐ฅ Type 'null' is not assignable to type 'ErrorType'.
} catch (error) {
return [error, null] // ๐ฅ Type 'null' is not assignable to type 'DataType'.
}
}
Why is this better?
const [error, user] = await until(() => fetchUser(id))
instead of this?
const [error, user] = await until(fetchUser(id))
The old versions of TS had trouble creating a discriminated union type out of an array tuple if you destructure it. That's not the case for the latest TS 4.9. We can use the array-based pattern again.
I saw someone using this library and it was confusing to me at first glance of what it was doing.
Then i figured: Is until
needed, are there alternative solutions? And there is...
This dose not do exactly the same thing until is doing, but you won't need any try/catch or dependencies with the Settle method.
const [{status, value}] = await Promise.allSettled([fetchUser(id)])
the output is either:
{ status: "fulfilled", value: user } // or
{ status: "rejected", reason: Error('an error') }
So the signature changes a bit. instead of checking for an error object u would have to check if status is fulfilled or rejected
- const [error, user] = await until(() => fetchUser(id))
-
- if (error) {
- return handleError(error)
- }
+ const [{status, value: user}] = await Promise.allSettled([fetchUser(id)])
+
+ if (status === 'rejected') {
+ return handleError(error)
+ }
user.password = 'abc'
await user.$save()
Sry if this is not an issue or bug, just wanted to share some stuff, you can close this - shares
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.