Skip to content

Commit

Permalink
wip(async): 新增 wait 方法,允许随机等待一段时间
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Jan 6, 2024
1 parent b62b7b2 commit b118d64
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@
"@jest/core": "^29",
"@jest/types": "^29",
"@lzwme/fed-lint-helper": "^2.5.1",
"@types/eslint": "^8.56.0",
"@types/eslint": "^8.56.1",
"@types/jest": "^29.5.11",
"@types/micromatch": "^4.0.6",
"@types/node": "^20.10.5",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"compressing": "^1.10.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^27.6.0",
"eslint-plugin-prettier": "^5.1.1",
"eslint-plugin-jest": "^27.6.1",
"eslint-plugin-prettier": "^5.1.2",
"eslint-plugin-unicorn": "^50.0.1",
"husky": "^8.0.3",
"jest": "^29.7.0",
Expand All @@ -87,7 +87,7 @@
"standard-version": "^9.5.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
"typedoc": "^0.25.4",
"typedoc": "^0.25.6",
"typescript": "^5.3.3",
"windows-process-tree": "^0.4.0"
},
Expand Down
15 changes: 13 additions & 2 deletions src/common/async.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* @Author: lzw
* @Date: 2022-01-12 15:10:41
* @LastEditors: lzw
* @LastEditTime: 2023-02-13 13:56:51
* @LastEditors: renxia
* @LastEditTime: 2024-01-06 15:12:13
* @Description:
* @see src\vs\base\common\async.ts
*/
Expand Down Expand Up @@ -377,6 +377,17 @@ export class AutoOpenBarrier extends Barrier {

export const sleep = <T>(milliseconds = 0, value?: T | (() => T | Promise<T>)): Promise<T | undefined> =>
new Promise(resolve => setTimeout(() => resolve(value instanceof Function ? value() : value), milliseconds));
/**
* 随机等待一定范围的时间(单位为毫秒)
* @param min 等待最小时间
* @param max 等待最大时间。若小于 min 则取值为为 max + min
* @returns 返回实际等待的时间
*/
export function wait(min = 0, max = 0) {
if (max > 0 && max < min) max += min;
const delay = max > min ? Math.round(Math.random() * (max - min)) + min : min;
return sleep(delay).then(() => delay);
}

export async function retry<T>(
task: ITask<Promise<T> | T>,
Expand Down

0 comments on commit b118d64

Please sign in to comment.