Skip to main content
added 206 characters in body
Source Link

I have a suggestion to properly handle rejects in a novel approach, without having multiple try-catch blocks. The advantage is that it doesn't break the control flow, preventing jumps from the try block to the catch block and making it easier to locate the source of the error other than navigating a stack trace.

import to from './to'; async foo(id: string): Promise<A> { let err, result; [err, result] = await to(someAsyncPromise()); // notice the to() here if (err) { return 400; } return 200; } 

Where the to.ts function should be imported from:

export default function to(promise: Promise<any>): Promise<any> { return promise.then(data => { return [null, data]; }).catch(err => [err]); } 

Credits go to Dima Grossman in the following link.

I have a suggestion to properly handle rejects in a novel approach, without having multiple try-catch blocks.

import to from './to'; async foo(id: string): Promise<A> { let err, result; [err, result] = await to(someAsyncPromise()); // notice the to() here if (err) { return 400; } return 200; } 

Where the to.ts function should be imported from:

export default function to(promise: Promise<any>): Promise<any> { return promise.then(data => { return [null, data]; }).catch(err => [err]); } 

Credits go to Dima Grossman in the following link.

I have a suggestion to properly handle rejects in a novel approach, without having multiple try-catch blocks. The advantage is that it doesn't break the control flow, preventing jumps from the try block to the catch block and making it easier to locate the source of the error other than navigating a stack trace.

import to from './to'; async foo(id: string): Promise<A> { let err, result; [err, result] = await to(someAsyncPromise()); // notice the to() here if (err) { return 400; } return 200; } 

Where the to.ts function should be imported from:

export default function to(promise: Promise<any>): Promise<any> { return promise.then(data => { return [null, data]; }).catch(err => [err]); } 

Credits go to Dima Grossman in the following link.

Source Link

I have a suggestion to properly handle rejects in a novel approach, without having multiple try-catch blocks.

import to from './to'; async foo(id: string): Promise<A> { let err, result; [err, result] = await to(someAsyncPromise()); // notice the to() here if (err) { return 400; } return 200; } 

Where the to.ts function should be imported from:

export default function to(promise: Promise<any>): Promise<any> { return promise.then(data => { return [null, data]; }).catch(err => [err]); } 

Credits go to Dima Grossman in the following link.