node_modules/p-timeout/index.js   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 2.2

Size

Lines of Code 36
Function Count 5

Duplication

Duplicated Lines 1
Ratio 2.78 %

Importance

Changes 0
Metric Value
cc 0
eloc 22
nc 8
dl 1
loc 36
rs 10
c 0
b 0
f 0
wmc 11
mnd 1
bc 6
fnc 5
bpm 1.2
cpm 2.2
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ ??? 0 24 1
A index.js ➔ constructor 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
'use strict';
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
const pFinally = require('p-finally');
3
4
class TimeoutError extends Error {
5
	constructor(message) {
6
		super(message);
7
		this.name = 'TimeoutError';
8
	}
9
}
10
11
module.exports = (promise, ms, fallback) => new Promise((resolve, reject) => {
12
	if (typeof ms !== 'number' || ms < 0) {
13
		throw new TypeError('Expected `ms` to be a positive number');
14
	}
15
16
	const timer = setTimeout(() => {
17
		if (typeof fallback === 'function') {
18
			resolve(fallback());
19
			return;
20
		}
21
22
		const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${ms} milliseconds`;
23
		const err = fallback instanceof Error ? fallback : new TimeoutError(message);
24
25
		reject(err);
26
	}, ms);
27
28
	pFinally(
29
		promise.then(resolve, reject),
30
		() => {
31
			clearTimeout(timer);
32
		}
33
	);
34
});
35
36
module.exports.TimeoutError = TimeoutError;
37