@trenskow/custom-promise 0.11.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,46 @@
1
+ import globals from 'globals';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import js from '@eslint/js';
5
+ import { FlatCompat } from '@eslint/eslintrc';
6
+
7
+ const __filename = fileURLToPath(import.meta.url);
8
+ const __dirname = path.dirname(__filename);
9
+ const compat = new FlatCompat({
10
+ baseDirectory: __dirname,
11
+ recommendedConfig: js.configs.recommended,
12
+ allConfig: js.configs.all
13
+ });
14
+
15
+ export default [...compat.extends('eslint:recommended'), {
16
+ languageOptions: {
17
+ globals: {
18
+ ...globals.node,
19
+ ...globals.mocha,
20
+ },
21
+
22
+ ecmaVersion: 'latest',
23
+ sourceType: 'module',
24
+ },
25
+
26
+ rules: {
27
+ indent: ['error', 'tab'],
28
+ 'linebreak-style': ['error', 'unix'],
29
+ quotes: ['error', 'single'],
30
+ semi: ['error', 'always'],
31
+
32
+ 'no-console': ['error', {
33
+ allow: ['warn', 'error', 'info'],
34
+ }],
35
+
36
+ 'no-unused-vars': ['error', {
37
+ argsIgnorePattern: '^_',
38
+ }],
39
+
40
+ 'no-empty': ['error', {
41
+ allowEmptyCatch: true,
42
+ }],
43
+
44
+ 'require-atomic-updates': 'off',
45
+ },
46
+ }];
package/index.js CHANGED
@@ -1,20 +1,20 @@
1
- 'use strict';
2
-
3
1
  class CustomPromise {
4
2
 
5
3
  constructor() {
6
4
  this._state = 'pending';
7
5
  this._result = undefined;
8
- this._resolveCallback = undefined;
9
- this._rejectCallback = undefined;
10
6
  this._finalizers = [];
7
+ this._onFulfilled = [];
8
+ this._onRejected = [];
11
9
  }
12
10
 
13
11
  _resolve(result) {
14
12
  if (this._state !== 'pending') return;
15
13
  this._state = 'fulfilled';
16
14
  this._result = result;
17
- this._resolveCallback?.(result);
15
+ this._onFulfilled
16
+ .filter((onFulfilled) => typeof onFulfilled === 'function')
17
+ .forEach((onFulfilled) => onFulfilled(result));
18
18
  this._finalizers.splice(0).forEach((onFinally) => onFinally());
19
19
  }
20
20
 
@@ -22,63 +22,25 @@ class CustomPromise {
22
22
  if (this._state !== 'pending') return;
23
23
  this._state = 'rejected';
24
24
  this._error = error;
25
- this._rejectCallback?.(error);
25
+ this._onRejected
26
+ .filter((onRejected) => typeof onRejected === 'function')
27
+ .forEach((onRejected) => onRejected(error));
26
28
  this._finalizers.splice(0).forEach((onFinally) => onFinally());
27
29
  }
28
30
 
29
31
  then(onFulfilled, onRejected) {
30
- return new Promise((resolve, reject) => {
31
-
32
- const resolveWrapper = (value) => {
33
- try {
34
-
35
- if (typeof onFulfilled !== 'function') {
36
- return resolve(value);
37
- }
38
-
39
- const result = onFulfilled(value);
40
-
41
- if (typeof result?.then === 'function') {
42
- return result.then(resolve, reject);
43
- }
44
-
45
- resolve(result);
46
-
47
- } catch (error) {
48
- reject(error);
49
- }
50
- };
51
32
 
52
- const rejectedWrapper = (error) => {
53
- try {
54
-
55
- if (typeof onRejected !== 'function') {
56
- return reject(error);
57
- }
58
-
59
- const result = onRejected(error);
60
-
61
- if (typeof result?.then === 'function') {
62
- return result.then(resolve, reject);
63
- }
64
-
65
- reject(result);
66
-
67
- } catch (error) {
68
- reject(error);
69
- }
70
- };
33
+ if (this._state === 'fulfilled') {
34
+ onFulfilled?.(this._result);
35
+ } else if (this._state === 'rejected') {
36
+ onRejected?.(this._error);
37
+ } else {
38
+ this._onFulfilled.push(onFulfilled);
39
+ this._onRejected.push(onRejected);
40
+ }
71
41
 
72
- if (this._state === 'fulfilled') {
73
- resolveWrapper(this._result);
74
- } else if (this._state === 'rejected') {
75
- rejectedWrapper(this._error);
76
- } else {
77
- this._resolveCallback = resolveWrapper;
78
- this._rejectCallback = rejectedWrapper;
79
- }
42
+ return this;
80
43
 
81
- });
82
44
  }
83
45
 
84
46
  finally(onFinally) {
@@ -91,4 +53,4 @@ class CustomPromise {
91
53
 
92
54
  }
93
55
 
94
- exports = module.exports = CustomPromise;
56
+ export default CustomPromise;
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@trenskow/custom-promise",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "description": "A extendable class for making custom promises.",
5
5
  "main": "index.js",
6
+ "type": "module",
6
7
  "directories": {
7
8
  "test": "test"
8
9
  },
@@ -22,5 +23,11 @@
22
23
  "bugs": {
23
24
  "url": "https://github.com/trenskow/custom-promise/issues"
24
25
  },
25
- "homepage": "https://github.com/trenskow/custom-promise#readme"
26
+ "homepage": "https://github.com/trenskow/custom-promise#readme",
27
+ "devDependencies": {
28
+ "@eslint/eslintrc": "^3.1.0",
29
+ "@eslint/js": "^9.13.0",
30
+ "eslint": "^9.13.0",
31
+ "globals": "^15.11.0"
32
+ }
26
33
  }
package/.eslintrc.js DELETED
@@ -1,49 +0,0 @@
1
- module.exports = {
2
- 'env': {
3
- 'es6': true,
4
- 'node': true,
5
- 'mocha': true
6
- },
7
- 'parserOptions': {
8
- 'ecmaVersion': 2017
9
- },
10
- 'extends': 'eslint:recommended',
11
- 'rules': {
12
- 'indent': [
13
- 'error',
14
- 'tab'
15
- ],
16
- 'linebreak-style': [
17
- 'error',
18
- 'unix'
19
- ],
20
- 'quotes': [
21
- 'error',
22
- 'single'
23
- ],
24
- 'semi': [
25
- 'error',
26
- 'always'
27
- ],
28
- 'no-console': [
29
- 'error', {
30
- 'allow': [
31
- 'warn',
32
- 'error',
33
- 'info'
34
- ]
35
- }
36
- ],
37
- 'no-unused-vars': [
38
- 'error', {
39
- 'argsIgnorePattern': '^_'
40
- }
41
- ],
42
- 'no-empty': [
43
- 'error', {
44
- 'allowEmptyCatch': true
45
- }
46
- ],
47
- 'require-atomic-updates': 'off'
48
- }
49
- };