Comparing version 0.1.0 to 0.2.0
@@ -1,3 +0,25 @@ | ||
declare function p<T>(asyncFunction: Promise<T>): Promise<[T, null] | [T, unknown]>; | ||
/** | ||
* Go inspired error handling for asynchronous functions. | ||
* | ||
* ### Example Usage | ||
* | ||
* ```ts | ||
* import p from "p-flat"; | ||
* | ||
* const [res, err] = await p(someAsyncWork(...args)); | ||
* | ||
* if (err !== null) { | ||
* // If `err` is not `null`, some error or value has been thrown | ||
* // `res` will be `null` and cannot be used safely here | ||
* // Error handling for `someAsyncWork` should be done here | ||
* console.error(err); | ||
* return; | ||
* } | ||
* | ||
* // Else, `res` will be the correct return type and value of `someAsyncWork` | ||
* console.log(res); | ||
* ``` | ||
*/ | ||
declare function p<T, U extends NonNullable<unknown>>(fn: Promise<T>): Promise<[T, null] | [null, U]>; | ||
export { p as default }; |
@@ -1,2 +0,2 @@ | ||
"use strict";async function p(r){try{return[await r,null]}catch(t){return[null,t]}}module.exports=p; | ||
"use strict";async function t(r){try{return[await r,null]}catch(n){return[null,n??new Error("[p-flat] unknown nullish error")]}}module.exports=t; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "p-flat", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"main": "dist/index.js", | ||
@@ -13,14 +13,19 @@ "module": "dist/index.mjs", | ||
], | ||
"scripts": { | ||
"build": "cross-env NODE_ENV=production rollup -c", | ||
"prepublish": "yarn build" | ||
}, | ||
"devDependencies": { | ||
"cross-env": "^7.0.3", | ||
"esbuild": "^0.12.18", | ||
"rollup": "^2.56.0", | ||
"esbuild": "^0.12.29", | ||
"rollup": "^2.79.1", | ||
"rollup-plugin-dts": "^3.0.2", | ||
"rollup-plugin-esbuild": "^4.5.0", | ||
"typescript": "^4.3.5" | ||
"rollup-plugin-esbuild": "^4.10.3", | ||
"typescript": "^4.9.4", | ||
"vitest": "^0.28.3" | ||
}, | ||
"scripts": { | ||
"dev": "vitest", | ||
"test": "vitest run", | ||
"typecheck": "tsc --noEmit", | ||
"ci": "pnpm test && pnpm typecheck", | ||
"build": "cross-env NODE_ENV=production rollup -c", | ||
"prepublish": "pnpm ci && pnpm build" | ||
} | ||
} | ||
} |
# `p-flat` | ||
No more callback and try-catch hell. | ||
Go inspired error handling for asynchronous functions. | ||
## Installation | ||
```sh | ||
npm install p-flat | ||
yarn add p-flat | ||
pnpm add p-flat | ||
``` | ||
## Example Usage | ||
@@ -12,5 +22,5 @@ | ||
if (err) { | ||
// If `err` is truthy, some error has been thrown | ||
// `res` will be `null` and should not be accessed/used | ||
if (err !== null) { | ||
// If `err` is not `null`, some error or value has been thrown | ||
// `res` will be `null` and cannot be used safely here | ||
// Error handling for `someAsyncWork` should be done here | ||
@@ -21,14 +31,11 @@ console.error(err); | ||
// Else, `res` will be the correct return value of `someAsyncWork` | ||
// Else, `res` will be the correct return type and value of `someAsyncWork` | ||
console.log(res); | ||
``` | ||
**Note:** | ||
- If `err` is not `null` (ie. the asynchronous function threw an error), then `res` will be `null` | ||
- If `err` is `null` (ie. the asynchronous function did not throw any errors), then `res` is guaranteed to have the return type and value of the resolved function | ||
- If `err` is truthy (ie. the asynchronous function threw an error), then `res` will be `null` | ||
- If `err` if falsy (ie. the asynchronous function did not throw any errors), then `res` is guaranteed to have the return value of the resolved function | ||
- Due to limitations in TypeScript, `res` will always have the return type of the resolved function, even when `err` is truthy (and thus `res` is `null`) | ||
## Rationale | ||
## Motivation | ||
Inspired from the [error handling in Go](https://blog.golang.org/error-handling-and-go), this construct greatly increases code readability. | ||
@@ -97,3 +104,3 @@ | ||
const [randomNumber, randomNumberErr] = await p(getRandomNumber()); | ||
if (randomNumberErr) { | ||
if (randomNumberErr !== null) { | ||
console.error(randomNumberErr); | ||
@@ -105,3 +112,3 @@ // Uncomment the next line to stop execution | ||
const [squareNumber, squareNumberErr] = await p(getSquareNumber(randomNumber)); | ||
if (squareNumberErr) { | ||
if (squareNumberErr !== null) { | ||
console.error(squareNumberErr); | ||
@@ -108,0 +115,0 @@ // Uncomment the next line to stop execution |
@@ -1,8 +0,30 @@ | ||
async function p<T>( | ||
asyncFunction: Promise<T> | ||
): Promise<[T, null] | [T, unknown]> { | ||
/** | ||
* Go inspired error handling for asynchronous functions. | ||
* | ||
* ### Example Usage | ||
* | ||
* ```ts | ||
* import p from "p-flat"; | ||
* | ||
* const [res, err] = await p(someAsyncWork(...args)); | ||
* | ||
* if (err !== null) { | ||
* // If `err` is not `null`, some error or value has been thrown | ||
* // `res` will be `null` and cannot be used safely here | ||
* // Error handling for `someAsyncWork` should be done here | ||
* console.error(err); | ||
* return; | ||
* } | ||
* | ||
* // Else, `res` will be the correct return type and value of `someAsyncWork` | ||
* console.log(res); | ||
* ``` | ||
*/ | ||
async function p<T, U extends NonNullable<unknown>>( | ||
fn: Promise<T> | ||
): Promise<[T, null] | [null, U]> { | ||
try { | ||
return [await asyncFunction, null]; | ||
} catch (error: unknown) { | ||
return [null as unknown as T, error]; | ||
return [await fn, null]; | ||
} catch (error) { | ||
return [null, error ?? new Error("[p-flat] unknown nullish error")]; | ||
} | ||
@@ -9,0 +31,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10519
10
119
117
7