Comments (9)
How about this #46?
from await-to-js.
use res!.data.everything
to get safe deeply access object, read more detail here https://codewithstyle.info/Deep-property-access-in-TypeScript/
from await-to-js.
@lucaspoffo I have not tried this myself but it looks like conditional types with arrays should be possible: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#example-2
from await-to-js.
async function testA() { const result = await to(successAsync()) if (result[0]) { console.log('Found error in successAsync') return // don't forget } console.log(result[1].value) // No type error } async function testB() { const [err, result] = await to(successAsync()) if (err) { console.log('Found error in successAsync') return } console.log(result.value) // Type error }
so is there any why to separate into two variables and show no err? the res[0] and res[1] is not quite clean for each time to evoke this function.
from await-to-js.
@Rizaldinurm using !
tells TS to ignore the type so when you use !
you lose type safety.
The ideal situation is that TS makes sure you have checked that error
is undefined
(or that res
is defined) before using res
.
from await-to-js.
@bezreyhan When the return type is an Array you cannot infer the values between each other,
but if the return type is an Object you can do it, like this:
async function to<T, U = Error>(promise: Promise<T>): Promise<{ error: U; data: undefined } | { error: null; data: T }> {
return promise.then((data: T) => ({ error: null, data }))
.catch((error: U) => ({ error, data: undefined }))
}
async function successAsync(): Promise<{ value: number }> { return { value: 5 } }
async function test() {
const result = await to(successAsync())
if (result.error) {
console.log('Found error in successAsync')
return // don't forget
}
console.log(result.data.value) // No type error
}
test()
from await-to-js.
@bezreyhan You're right, arrays works too, but you can't separate in 2 variables:
async function to<T, U = Error>(promise: Promise<T>): Promise<[U, undefined] | [null, T]> {
return promise.then<[null, T]>((data) => ([null, data]))
.catch<[U, undefined]>((error: U) => [error, undefined])
}
async function successAsync(): Promise<{ value: number }> { return { value: 5 } }
async function testA() {
const result = await to(successAsync())
if (result[0]) {
console.log('Found error in successAsync')
return // don't forget
}
console.log(result[1].value) // No type error
}
async function testB() {
const [err, result] = await to(successAsync())
if (err) {
console.log('Found error in successAsync')
return
}
console.log(result.value) // Type error
}
from await-to-js.
@JFGHT I haven't tested it but that look good to me!
from await-to-js.
I think this can now be closed because v3.0.0 with the fixes in #46 was released yesterday.
from await-to-js.
Related Issues (20)
- Error while using KnexJS HOT 1
- Idiomatic go-lang motivation is not readily avaible HOT 1
- No function syntax provided, no errorExt explanation provided HOT 1
- Every CATCH has an IF HOT 11
- ESLint prints a warning if error isn't used. HOT 2
- thanks author, you may want to use decorater to catch your error~ HOT 1
- Does not work with Promise.promisify HOT 2
- Not catching sync errors
- Runs twice HOT 1
- promise.reject() no params error always undefined HOT 2
- π-er Name: await-try-catch HOT 2
- Why version 3? HOT 4
- How to use this with mongoose's Query type? HOT 1
- TypeError: to is not a function HOT 3
- Error not set correctly HOT 2
- ε¦ζζ³θ·εerrorιι’ηδΏ‘ζ―ζδΉεοΌ HOT 1
- what's difference between v2 and v3 HOT 1
- Add named export for more convenient usage for applications using ES modules HOT 1
- No default error when promise.reject() no params
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from await-to-js.