-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.js
51 lines (38 loc) · 1.08 KB
/
tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import tap from "tap";
import { createWebSocket } from "./fake-websocket.js";
tap.test("send event from source to sink", (t) => {
const { WebSocket, Server } = createWebSocket();
Server.on("connection", (socket) => {
socket.send("test message");
});
const client1 = new WebSocket();
client1.on("message", (msg) => {
t.equal(msg, "test message");
});
const client2 = new WebSocket();
client2.on("message", (msg) => {
t.equal(msg, "test message");
t.end();
});
});
///////////////////////////////////////////////////////
export default async function pWaitFor(condition, options = {}) {
const { interval, timeout, before } = options;
let retryTimeout;
const promise = new Promise((resolve, reject) => {
const check = async () => {
try {
const value = await condition();
if (value === true) {
resolve();
} else {
retryTimeout = setTimeout(check, interval);
}
} catch (error) {
reject(error);
}
};
retryTimeout = setTimeout(check, interval);
});
return promise;
}