@trenskow/wait 1.2.8 → 1.3.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.
package/.eslintrc.cjs ADDED
@@ -0,0 +1,63 @@
1
+ module.exports = {
2
+ 'env': {
3
+ 'es6': true,
4
+ 'node': true,
5
+ 'mocha': true
6
+ },
7
+ 'parserOptions': {
8
+ 'ecmaVersion': 2022,
9
+ 'sourceType': 'module'
10
+ },
11
+ 'extends': 'eslint:recommended',
12
+ 'rules': {
13
+ 'indent': [
14
+ 'error',
15
+ 'tab',
16
+ { 'SwitchCase': 1 }
17
+ ],
18
+ 'linebreak-style': [
19
+ 'error',
20
+ 'unix'
21
+ ],
22
+ 'quotes': [
23
+ 'error',
24
+ 'single'
25
+ ],
26
+ 'semi': [
27
+ 'error',
28
+ 'always'
29
+ ],
30
+ 'no-console': [
31
+ 'error', {
32
+ 'allow': [
33
+ 'warn',
34
+ 'error',
35
+ 'info'
36
+ ]
37
+ }
38
+ ],
39
+ 'no-unused-vars': [
40
+ 'error', {
41
+ 'argsIgnorePattern': '^_'
42
+ }
43
+ ],
44
+ 'no-empty': [
45
+ 'error', {
46
+ 'allowEmptyCatch': true
47
+ }
48
+ ],
49
+ 'no-trailing-spaces': [
50
+ 'error', {
51
+ 'ignoreComments': true
52
+ }
53
+ ],
54
+ 'require-atomic-updates': 'off',
55
+ 'no-implicit-globals': [
56
+ 'error'
57
+ ],
58
+ 'eol-last': [
59
+ 'error',
60
+ 'always'
61
+ ]
62
+ }
63
+ };
@@ -0,0 +1,22 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type": "node",
9
+ "request": "launch",
10
+ "name": "Launch Program",
11
+ "skipFiles": [
12
+ "<node_internals>/**"
13
+ ],
14
+ "program": "${workspaceFolder}/node_modules/mocha/bin/mocha",
15
+ "args": [
16
+ "${workspaceFolder}/test/index.js",
17
+ "--timeout",
18
+ "0"
19
+ ]
20
+ }
21
+ ]
22
+ }
package/index.js CHANGED
@@ -1,9 +1,14 @@
1
- 'use strict';
1
+ //
2
+ // index.js
3
+ // @trenskow/app
4
+ //
5
+ // Created by Kristian Trenskow on 2023/07/12
6
+ // For license see LICENSE.
7
+ //
2
8
 
3
- const
4
- { duration } = require('@trenskow/units');
9
+ import { duration } from '@trenskow/units';
5
10
 
6
- module.exports = exports = (interval) => {
11
+ const wait = (interval) => {
7
12
 
8
13
  let timeoutId;
9
14
  let resolver;
@@ -21,5 +26,21 @@ module.exports = exports = (interval) => {
21
26
 
22
27
  result.elapse = result.cancel;
23
28
 
29
+ result.delayed = async (todo) => {
30
+
31
+ let todoResult = (await Promise.allSettled([
32
+ Promise.resolve(todo(result.cancel)),
33
+ result
34
+ ]))[0];
35
+
36
+ if (todoResult.status === 'rejected') throw todoResult.reason;
37
+
38
+ return todoResult.value;
39
+
40
+ };
41
+
24
42
  return result;
43
+
25
44
  };
45
+
46
+ export default wait;
package/package.json CHANGED
@@ -1,33 +1,35 @@
1
1
  {
2
- "name": "@trenskow/wait",
3
- "version": "1.2.8",
4
- "description": "A promisified version of `setTimeout`.",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "./node_modules/mocha/bin/mocha.js test/index.js"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/trenskow/wait.git"
12
- },
13
- "keywords": [
14
- "sleep",
15
- "wait",
16
- "setTimeout",
17
- "promise"
18
- ],
19
- "author": "Kristian Trenskow <trenskow@me.com>",
20
- "license": "MIT",
21
- "bugs": {
22
- "url": "https://github.com/trenskow/wait/issues"
23
- },
24
- "homepage": "https://github.com/trenskow/wait#readme",
25
- "dependencies": {
26
- "@trenskow/units": "^0.1.6"
27
- },
28
- "devDependencies": {
29
- "chai": "^4.3.7",
30
- "chai-as-promised": "^7.1.1",
31
- "mocha": "^10.2.0"
32
- }
2
+ "name": "@trenskow/wait",
3
+ "version": "1.3.0",
4
+ "description": "A promisified version of `setTimeout`.",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "./node_modules/mocha/bin/mocha.js test/index.js"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/trenskow/wait.git"
13
+ },
14
+ "keywords": [
15
+ "sleep",
16
+ "wait",
17
+ "setTimeout",
18
+ "promise"
19
+ ],
20
+ "author": "Kristian Trenskow <trenskow@me.com>",
21
+ "license": "MIT",
22
+ "bugs": {
23
+ "url": "https://github.com/trenskow/wait/issues"
24
+ },
25
+ "homepage": "https://github.com/trenskow/wait#readme",
26
+ "dependencies": {
27
+ "@trenskow/units": "^0.1.7"
28
+ },
29
+ "devDependencies": {
30
+ "chai": "^4.3.7",
31
+ "chai-as-promised": "^7.1.1",
32
+ "eslint": "^8.44.0",
33
+ "mocha": "^10.2.0"
34
+ }
33
35
  }
package/test/index.js CHANGED
@@ -1,10 +1,19 @@
1
- 'use strict';
1
+ //
2
+ // index.js
3
+ // @trenskow/wait
4
+ //
5
+ // Created by Kristian Trenskow on 2023/07/12
6
+ // For license see LICENSE.
7
+ //
2
8
 
3
- require('chai').use(require('chai-as-promised'));
9
+ import chai from 'chai';
10
+ import chaiAsPromised from 'chai-as-promised';
4
11
 
5
- const { expect } = require('chai');
12
+ import wait from '../index.js';
6
13
 
7
- const wait = require('../');
14
+ const { expect } = chai;
15
+
16
+ chai.use(chaiAsPromised);
8
17
 
9
18
  describe('wait', () => {
10
19
  it('should wait one seconds (string).', () => {
@@ -33,4 +42,28 @@ describe('wait', () => {
33
42
  }))
34
43
  .to.eventually.be.satisfies((val) => val >= 200 && val <= 250);
35
44
  });
45
+ it ('delayed function should return the correct value.', () => {
46
+ const startTime = new Date();
47
+ return expect(
48
+ wait(1000).delayed(async () => {
49
+ return 'Hello, World!';
50
+ }).then((result) => {
51
+ expect((new Date()).getTime() - startTime.getTime()).to.satisfies((val) => val >= 1000);
52
+ return result;
53
+ }))
54
+ .to.eventually.equal('Hello, World!');
55
+
56
+ });
57
+ it ('delayed function should throw an error.', () => {
58
+ const startTime = new Date();
59
+ return expect(
60
+ wait(1000).delayed(async () => {
61
+ throw new Error('This is an error!');
62
+ }).catch((error) => {
63
+ expect((new Date()).getTime() - startTime.getTime()).to.satisfies((val) => val >= 1000);
64
+ throw error;
65
+ }))
66
+ .to.eventually.be.rejectedWith('This is an error!');
67
+
68
+ });
36
69
  });
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
- };