on-error-resume-next
Advanced tools
Comparing version 1.2.1-master.657032b to 2.0.0-main.03bf411
{ | ||
"name": "on-error-resume-next", | ||
"version": "1.2.1-master.657032b", | ||
"description": "", | ||
"main": "lib/index.js", | ||
"version": "2.0.0-main.03bf411", | ||
"description": "Run a function, synchronously or asynchronously, and ignore errors.", | ||
"files": [ | ||
"lib/**/*" | ||
"./dist/" | ||
], | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/on-error-resume-next.d.mts", | ||
"default": "./dist/on-error-resume-next.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/on-error-resume-next.d.ts", | ||
"default": "./dist/on-error-resume-next.js" | ||
} | ||
}, | ||
"./async": { | ||
"import": { | ||
"types": "./dist/on-error-resume-next.async.d.mts", | ||
"default": "./dist/on-error-resume-next.async.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/on-error-resume-next.async.d.ts", | ||
"default": "./dist/on-error-resume-next.async.js" | ||
} | ||
}, | ||
"./auto": { | ||
"import": { | ||
"types": "./dist/on-error-resume-next.auto.d.mts", | ||
"default": "./dist/on-error-resume-next.auto.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/on-error-resume-next.auto.d.ts", | ||
"default": "./dist/on-error-resume-next.auto.js" | ||
} | ||
} | ||
}, | ||
"main": "./dist/on-error-resume-next.js", | ||
"typings": "./dist/on-error-resume-next.d.ts", | ||
"scripts": { | ||
"build": "babel --out-dir lib src --ignore **/*.spec.js,**/*.test.js", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls", | ||
"prestart": "npm run build:babel", | ||
"start": "npm run build:babel -- --skip-initial-build --watch", | ||
"test": "jest --collectCoverage true" | ||
"build": "tsup", | ||
"bump": "npm run bump:prod && npm run bump:dev && npm run bump:auditfix", | ||
"bump:auditfix": "npm audit fix || exit 0", | ||
"bump:dev": "PACKAGES_TO_BUMP=$(cat package.json | jq -r '(.pinDependencies // {}) as $P | (.localPeerDependencies // {}) as $L | (.devDependencies // {}) | to_entries | map(select(.key as $K | $L | has($K) | not)) | map(.key + \"@\" + ($P[.key] // [\"latest\"])[0]) | join(\" \")') && [ ! -z \"$PACKAGES_TO_BUMP\" ] && npm install $PACKAGES_TO_BUMP || true", | ||
"bump:prod": "PACKAGES_TO_BUMP=$(cat package.json | jq -r '(.pinDependencies // {}) as $P | (.localPeerDependencies // {}) as $L | (.dependencies // {}) | to_entries | map(select(.key as $K | $L | has($K) | not)) | map(.key + \"@\" + ($P[.key] // [\"latest\"])[0]) | join(\" \")') && [ ! -z \"$PACKAGES_TO_BUMP\" ] && npm install $PACKAGES_TO_BUMP || true", | ||
"precommit": "npm run precommit:eslint && npm run precommit:typescript:production && npm run precommit:typescript:test", | ||
"precommit:eslint": "eslint ./src/", | ||
"precommit:typescript:production": "tsc --noEmit --project ./src/tsconfig.precommit.production.json", | ||
"precommit:typescript:test": "tsc --noEmit --project ./src/tsconfig.precommit.test.json", | ||
"prepack": "cp ../../CHANGELOG.md . && cp ../../LICENSE . && cp ../../README.md .", | ||
"switch": "cat package.json | jq --arg SWITCH_NAME $SWITCH_NAME -r '(.[\"switch:\" + $SWITCH_NAME] // {}) as $TEMPLATE | .devDependencies += ($TEMPLATE.devDependencies // {}) | .dependencies += ($TEMPLATE.dependencies // {})' | tee ./package.json.tmp && mv ./package.json.tmp ./package.json", | ||
"test": "jest" | ||
}, | ||
@@ -20,3 +60,7 @@ "repository": { | ||
}, | ||
"author": "William Wong <compulim@hotmail.com> (http://compulim.info/)", | ||
"keywords": [ | ||
"message-port", | ||
"rpc" | ||
], | ||
"author": "William Wong (https://github.com/compulim)", | ||
"license": "MIT", | ||
@@ -27,15 +71,20 @@ "bugs": { | ||
"homepage": "https://github.com/compulim/on-error-resume-next#readme", | ||
"jest": { | ||
"collectCoverage": true | ||
"pinDependencies": {}, | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.24.6", | ||
"@babel/preset-typescript": "^7.24.6", | ||
"@jest/globals": "^29.7.0", | ||
"@tsconfig/recommended": "^1.0.6", | ||
"@tsconfig/strictest": "^2.0.5", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^20.12.13", | ||
"esbuild": "^0.21.4", | ||
"jest": "^29.7.0", | ||
"prettier": "^3.2.5", | ||
"tsup": "^8.0.2", | ||
"typescript": "^5.4.5" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.8.4", | ||
"@babel/core": "^7.9.6", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.9.6", | ||
"@babel/preset-env": "^7.9.6", | ||
"babel-jest": "^26.0.1", | ||
"babel-plugin-add-module-exports": "^1.0.2", | ||
"jest": "^26.0.1", | ||
"regenerator-runtime": "^0.13.5" | ||
"dependencies": { | ||
"on-error-resume-next": "^2.0.0-main.03bf411" | ||
} | ||
} |
101
README.md
@@ -5,3 +5,3 @@ # on-error-resume-next | ||
[![npm version](https://badge.fury.io/js/on-error-resume-next.svg)](https://badge.fury.io/js/on-error-resume-next) [![Build Status](https://travis-ci.org/compulim/on-error-resume-next.svg?branch=master)](https://travis-ci.org/compulim/on-error-resume-next) | ||
[![npm version](https://badge.fury.io/js/on-error-resume-next.svg)](https://npmjs.com/package/on-error-resume-next) | ||
@@ -12,2 +12,17 @@ The name come from [Visual Basic](https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/on-error-statement). The original `On Error Resume Next` statement is considered a bad error handling practice. | ||
When using `onErrorResumeNext`, please be responsible and fully understand the impact of ignoring errors. | ||
# Breaking changes | ||
## New in 2.0 | ||
We introduced named exports and removed default imports. The default is synchronous. The "auto-detection" version is being moved to under 'on-error-resume-next/auto'. | ||
```diff | ||
- import onErrorResumeNext from 'on-error-resume-next'; | ||
+ import { onErrorResumeNext } from 'on-error-resume-next/auto'; | ||
``` | ||
It is recommended to use either synchronous or asynchronous version for better clarity. | ||
# Usage | ||
@@ -18,32 +33,90 @@ | ||
```js | ||
import onErrorResumeNext from 'on-error-resume-next'; | ||
import { onErrorResumeNext } from 'on-error-resume-next'; | ||
const text = '{"hello":"World!"}'; | ||
const parsed = onErrorResumeNext(() => JSON.parse(text)); | ||
// Will return result on returns. | ||
const returned = onErrorResumeNext(() => JSON.parse('{"hello":"World!"}')); | ||
expect(parsed).toEqual({ hello: 'World!' }); | ||
expect(returned).toEqual({ hello: 'World!' }); | ||
// Will return undefined on throws. | ||
const thrown = onErrorResumeNext(() => JSON.parse('<xml />')); | ||
expect(thrown).toBeUndefined(); | ||
``` | ||
Otherwise, it will return `undefined`, | ||
Notes: if an asynchronous function is being passed to `onErrorResumeNext()`, it will throw to protect from false negatives. Please use `on-error-resume-next/async` for asynchronous functions. | ||
## Asynchronous using `async`/`await` | ||
`onErrorResumeNext` will capture both exceptions (synchronous) and rejections (asynchronous). The returned value is always a `Promise` object. | ||
```js | ||
const parsed = onErrorResumeNext(() => JSON.parse('<xml />')); | ||
import { onErrorResumeNext } from 'on-error-resume-next/async'; | ||
expect(parsed).toBeUndefined(); | ||
// "async" will return Promise on resolves. | ||
const resolution = onErrorResumeNext(() => Promise.resolve('Hello, World!')); | ||
await expect(resolution).resolves.toBe('Hello, World!'); | ||
// "async" will return Promise on returns. | ||
const returned = onErrorResumeNext(() => 'Hello, World!'); | ||
await expect(returned).resolves.toBe('Hello, World!'); | ||
// "async" will return Promise on rejects. | ||
const rejection = onErrorResumeNext(() => Promise.reject(new Error())); | ||
await expect(rejection).resolves.toBeUndefined(); | ||
// "async" will return Promise on throws. | ||
const thrown = onErrorResumeNext(() => { | ||
throw new Error(); | ||
}); | ||
await expect(thrown).resolves.toBeUndefined(); | ||
``` | ||
> When using `onErrorResumeNext`, please be responsible and fully understand the impact of ignoring errors. | ||
## Auto-detecting synchronous/asynchronous functions | ||
## Asynchronous using async/await | ||
> For best experience, please use synchronous or asynchronous version instead. | ||
`onErrorResumeNext` will capture both exceptions (synchronous) and rejections (asynchronous). It is recommended to pair up with `await` keyword to handle both cases in the same way. | ||
`on-error-resume-next/auto` will handle both exceptions (synchronous) and rejections (asynchronous) accordingly. | ||
```js | ||
const res = await onErrorResumeNext(() => fetch('/health')); | ||
import { onErrorResumeNext } from 'on-error-resume-next/auto'; | ||
expect(res).toBeUndefined(); | ||
// "auto" will return result on returns. | ||
const returned = onErrorResumeNext(() => 'Hello, World!'); | ||
expect(returned).toEqual('Hello, World!'); | ||
// "auto" will return undefined on throws. | ||
const thrown = onErrorResumeNext(() => { | ||
throw new Error('Hello, World!'); | ||
}); | ||
expect(thrown).toEqual(undefined); | ||
// "auto" will return Promise on resolves. | ||
const resolution = onErrorResumeNext(() => Promise.resolve('Hello, World!')); | ||
await expect(resolution).resolves.toBe('Hello, World!'); | ||
// "auto" will return Promise on rejects. | ||
const rejection = onErrorResumeNext(() => Promise.reject(new Error())); | ||
await expect(rejection).resolves.toBeUndefined(); | ||
``` | ||
> The code is for elaboration only, the better expectation should be `expect(res).resolves.toBeUndefined()`. | ||
## Sync vs. async vs. auto | ||
The following table show how each version react with different passing functions. | ||
| | Default (sync) | Async | Auto | | ||
| -------------------- | ------------------------- | ---------------------------- | ---------------------------- | | ||
| `return 1` | `1` | `Promise.resolve(1)` | `1` | | ||
| `throw 2` | `undefined` | `Promise.resolve(undefined)` | `undefined` | | ||
| `Promise.resolve(3)` | Not supported, will throw | `Promise.resolve(3)` | `Promise.resolve(3)` | | ||
| `Promise.reject(4)` | Not supported, will throw | `Promise.resolve(undefined)` | `Promise.resolve(undefined)` | | ||
# Contributions | ||
@@ -50,0 +123,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
26466
23
206
126
1
12
1