Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

on-error-resume-next

Package Overview
Dependencies
Maintainers
2
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

on-error-resume-next - npm Package Compare versions

Comparing version 2.0.0-main.8668b8f to 2.0.0-main.f3e7115

dist/chunk-MBTBSBLB.mjs

14

dist/on-error-resume-next.async.js

@@ -26,4 +26,16 @@ "use strict";

module.exports = __toCommonJS(index_async_exports);
var import_types = require("util/types");
function onErrorResumeNext(fn, context) {
return new Promise((resolve) => fn.call(context).then(resolve, () => resolve(void 0)));
return new Promise((resolve) => {
try {
const result = fn.call(context);
if ((0, import_types.isPromise)(result)) {
result.then(resolve, () => resolve(void 0));
} else {
resolve(result);
}
} catch {
resolve(void 0);
}
});
}

@@ -30,0 +42,0 @@ // Annotate the CommonJS export names for ESM import in node:

@@ -26,8 +26,20 @@ "use strict";

module.exports = __toCommonJS(src_exports);
// src/private/isPromise.ts
function isPromise(value) {
return !!((typeof value === "function" || typeof value === "object") && value && "then" in value && typeof value.then === "function");
}
// src/index.ts
function onErrorResumeNext(fn, context) {
let result;
try {
return fn.call(context);
result = fn.call(context);
} catch {
return;
return void 0;
}
if (isPromise(result)) {
throw new Error('Promise is not supported, please use "on-error-resume-next/async" instead.');
}
return result;
}

@@ -34,0 +46,0 @@ // Annotate the CommonJS export names for ESM import in node:

{
"name": "on-error-resume-next",
"version": "2.0.0-main.8668b8f",
"version": "2.0.0-main.f3e7115",
"description": "Run a function, synchronously or asynchronously, and ignore errors.",

@@ -28,2 +28,12 @@ "files": [

}
},
"./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"
}
}

@@ -52,4 +62,7 @@ },

"keywords": [
"message-port",
"rpc"
"basic",
"error",
"on error",
"vb",
"visual basic"
],

@@ -78,4 +91,4 @@ "author": "William Wong (https://github.com/compulim)",

"dependencies": {
"on-error-resume-next": "^2.0.0-main.8668b8f"
"on-error-resume-next": "^2.0.0-main.f3e7115"
}
}

@@ -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,4 +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

@@ -20,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).
`on-error-resume-next/auto` will handle both exceptions (synchronous) and rejections (asynchronous) accordingly.
```js
import onErrorResumeNext from 'on-error-resume-next/async';
import { onErrorResumeNext } from 'on-error-resume-next/auto';
const res = await onErrorResumeNext(() => fetch('/health'));
// "auto" will return result on returns.
const returned = onErrorResumeNext(() => 'Hello, World!');
await expect(res).resolves.toBeUndefined();
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();
```
## 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

@@ -52,0 +123,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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc