Skip to content

Commit 5bd3ad4

Browse files
fregantesindresorhus
authored andcommitted
Add timeout option (sindresorhus#21)
1 parent 08fc8ec commit 5bd3ad4

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ declare namespace elementReady {
99
*/
1010
readonly target?: Element | Document;
1111

12+
/**
13+
Milliseconds to wait before stopping the search and resolving the promise to `undefined`.
14+
15+
@default Infinity
16+
*/
17+
readonly timeout?: number
18+
1219
/**
1320
Automatically stop checking for the element to be ready after the DOM ready event. The promise is then resolved to `undefined`.
1421

index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ const pDefer = require('p-defer');
55

66
const cache = new ManyKeysMap();
77

8-
const elementReady = (selector, options = {}) => {
9-
const {
10-
target = document,
11-
stopOnDomReady = true
12-
} = options;
13-
14-
const cacheKeys = [target, selector, stopOnDomReady];
8+
const elementReady = (selector, {
9+
target = document,
10+
stopOnDomReady = true,
11+
timeout = Infinity
12+
} = {}) => {
13+
const cacheKeys = [target, selector, stopOnDomReady, timeout];
1514
const cachedPromise = cache.get(cacheKeys);
1615
if (cachedPromise) {
1716
return cachedPromise;
@@ -36,6 +35,10 @@ const elementReady = (selector, options = {}) => {
3635
})();
3736
}
3837

38+
if (timeout !== Infinity) {
39+
setTimeout(stop, timeout);
40+
}
41+
3942
// Interval to keep checking for it to come into the DOM
4043
(function check() {
4144
const element = target.querySelector(selector);

index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import elementReady = require('.');
44
const promise = elementReady('#unicorn');
55
elementReady('#unicorn', {target: document});
66
elementReady('#unicorn', {target: document.documentElement});
7+
elementReady('#unicorn', {timeout: 1000000});
78

89
elementReady('#unicorn', {stopOnDomReady: false});
910

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ Default: `true`
5454

5555
Automatically stop checking for the element to be ready after the [DOM ready event](https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event). The promise is then resolved to `undefined`.
5656

57+
##### timeout
58+
59+
Type: `number`<br>
60+
Default: `Infinity`
61+
62+
Milliseconds to wait before stopping the search and resolving the promise to `undefined`.
63+
5764
### elementReadyPromise#stop()
5865

5966
Type: `Function`

test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,36 @@ test('check if element ready before dom loaded', async t => {
103103
t.is(await elementCheck, element);
104104
});
105105

106+
test('check if element ready after timeout', async t => {
107+
const elementCheck = elementReady('#cheezburger', {
108+
stopOnDomReady: false,
109+
timeout: 1000
110+
});
111+
112+
// The element will be added eventually, but we're not around to wait for it
113+
setTimeout(() => {
114+
const element = document.createElement('p');
115+
element.id = 'cheezburger';
116+
document.body.append(element);
117+
}, 50000);
118+
119+
const element = await elementCheck;
120+
t.is(element, undefined);
121+
});
122+
123+
test('check if element ready before timeout', async t => {
124+
const element = document.createElement('p');
125+
element.id = 'thunders';
126+
document.body.append(element);
127+
128+
const elementCheck = elementReady('#thunders', {
129+
stopOnDomReady: false,
130+
timeout: 10
131+
});
132+
133+
t.is(await elementCheck, element);
134+
});
135+
106136
test('ensure only one promise is returned on multiple calls passing the same selector', t => {
107137
const elementCheck = elementReady('#not-found', {stopOnDomReady: false});
108138

0 commit comments

Comments
 (0)