Skip to content

Commit 291044d

Browse files
committed
Add waitUntil
1 parent 762def4 commit 291044d

6 files changed

Lines changed: 155 additions & 5 deletions

File tree

DOC.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4882,8 +4882,8 @@ Unlike filter, this method mutates array.
48824882

48834883
|Name |Type |Desc |
48844884
|---------|--------|------------------------------------|
4885-
|obj |array |Collection to iterate over |
4886-
|predicate|function|Function invoked per iteration |
4885+
|list |array |Collection to iterate over |
4886+
|iterator |function|Function invoked per iteration |
48874887
|[context]|* |Predicate context |
48884888
|return |array |Array of all values that are removed|
48894889

@@ -6009,6 +6009,24 @@ vlq.decode('2H'); // -> [123]
60096009
vlq.decode('2HwcqxB'); // -> [123, 456, 789]
60106010
```
60116011

6012+
## waitUntil
6013+
6014+
Wait until function returns a truthy value.
6015+
6016+
|Name |Type |Desc |
6017+
|--------------|--------|------------------|
6018+
|condition |function|Condition function|
6019+
|[timeout=0] |number |Timeout |
6020+
|[interval=250]|number |Wait interval |
6021+
6022+
```javascript
6023+
let a = 5;
6024+
setTimeout(() => a = 10, 500);
6025+
waitUntil(() => a === 10).then(() => {
6026+
console.log(a); // -> 10
6027+
});
6028+
```
6029+
60126030
## waterfall
60136031

60146032
Run an array of functions in series.

DOC_CN.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4876,8 +4876,8 @@ reject([1, 2, 3, 4, 5], function (val) {
48764876

48774877
|参数名|类型|说明|
48784878
|-----|----|---|
4879-
|obj|array|要遍历的集合|
4880-
|predicate|function|真值检测函数|
4879+
|list|array|要遍历的集合|
4880+
|iterator|function|真值检测函数|
48814881
|[context]|*|函数上下文|
48824882
|返回值|array|包含所有删除元素的数组|
48834883

@@ -6003,6 +6003,24 @@ vlq.decode('2H'); // -> [123]
60036003
vlq.decode('2HwcqxB'); // -> [123, 456, 789]
60046004
```
60056005

6006+
## waitUntil
6007+
6008+
等待直到条件函数返回真值。
6009+
6010+
|参数名|类型|说明|
6011+
|-----|----|---|
6012+
|condition|function|条件函数|
6013+
|[timeout=0]|number|超时|
6014+
|[interval=250]|number|等待间隔|
6015+
6016+
```javascript
6017+
let a = 5;
6018+
setTimeout(() => a = 10, 500);
6019+
waitUntil(() => a === 10).then(() => {
6020+
console.log(a); // -> 10
6021+
});
6022+
```
6023+
60066024
## waterfall
60076025

60086026
按顺序执行函数序列。

index.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2285,7 +2285,8 @@
22852285
"remove": {
22862286
"description": "Remove all elements from array that predicate returns truthy for and return an array of the removed elements.",
22872287
"dependencies": [
2288-
"safeCb"
2288+
"safeCb",
2289+
"types"
22892290
],
22902291
"env": "all",
22912292
"test": "all"
@@ -2880,6 +2881,14 @@
28802881
"env": "all",
28812882
"test": "all"
28822883
},
2884+
"waitUntil": {
2885+
"description": "Wait until function returns a truthy value.",
2886+
"dependencies": [
2887+
"now"
2888+
],
2889+
"env": "all",
2890+
"test": "all"
2891+
},
28832892
"waterfall": {
28842893
"description": "Run an array of functions in series.",
28852894
"dependencies": [

src/w/waitUntil.i18n.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## CN
2+
3+
等待直到条件函数返回真值。
4+
5+
|参数名|类型|说明|
6+
|-----|----|---|
7+
|condition|function|条件函数|
8+
|[timeout=0]|number|超时|
9+
|[interval=250]|number|等待间隔|

src/w/waitUntil.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Wait until function returns a truthy value.
2+
*
3+
* |Name |Type |Desc |
4+
* |--------------|--------|------------------|
5+
* |condition |function|Condition function|
6+
* |[timeout=0] |number |Timeout |
7+
* |[interval=250]|number |Wait interval |
8+
*/
9+
10+
/* example
11+
* let a = 5;
12+
* setTimeout(() => a = 10, 500);
13+
* waitUntil(() => a === 10).then(() => {
14+
* console.log(a); // -> 10
15+
* });
16+
*/
17+
18+
/* module
19+
* env: all
20+
* test: all
21+
*/
22+
23+
/* typescript
24+
* export declare function waitUntil(
25+
* condition: Function,
26+
* timeout?: number,
27+
* interval?: number
28+
* ): Promise<any>;
29+
*/
30+
31+
_('now');
32+
33+
exports = function(condition, timeout = 0, interval = 250) {
34+
function evalCondition() {
35+
return new Promise((resolve, reject) => {
36+
try {
37+
resolve(condition());
38+
} catch (e) {
39+
reject(e);
40+
}
41+
});
42+
}
43+
44+
return new Promise((resolve, reject) => {
45+
const startTime = now();
46+
const pollCondition = () => {
47+
evalCondition().then(val => {
48+
const elapsed = now() - startTime;
49+
if (val) {
50+
resolve(val);
51+
} else if (timeout && elapsed >= timeout) {
52+
reject(Error(`Wait timed out after ${elapsed} ms`));
53+
} else {
54+
setTimeout(pollCondition, interval);
55+
}
56+
}, reject);
57+
};
58+
pollCondition();
59+
});
60+
};

src/w/waitUntil.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const now = util.now;
2+
3+
it('basic', async () => {
4+
let a = 5;
5+
setTimeout(() => (a = 10), 500);
6+
await waitUntil(() => a === 10);
7+
expect(a).to.equal(10);
8+
});
9+
10+
it('timeout', async () => {
11+
let a = 5;
12+
setTimeout(() => (a = 10), 500);
13+
let err;
14+
try {
15+
await waitUntil(() => a === 10, 20);
16+
} catch (e) {
17+
err = e;
18+
}
19+
expect(err).to.be.an('error');
20+
});
21+
22+
it('interval', async () => {
23+
const startTime = now();
24+
let a = 5;
25+
setTimeout(() => (a = 10), 20);
26+
await waitUntil(() => a === 10, 0, 20);
27+
expect(a).to.equal(10);
28+
expect(now() - startTime).to.be.below(50);
29+
});
30+
31+
it('async', async () => {
32+
let a = 5;
33+
setTimeout(() => (a = 10), 500);
34+
await waitUntil(async () => a === 10);
35+
expect(a).to.equal(10);
36+
});

0 commit comments

Comments
 (0)