@mrtujiawei/utils
Advanced tools
Comparing version 1.4.8 to 1.4.9
{ | ||
"name": "@mrtujiawei/utils", | ||
"version": "1.4.8", | ||
"version": "1.4.9", | ||
"description": "把自己写的工具函数打包发布到npm上", | ||
@@ -5,0 +5,0 @@ "types": "types/index.d.ts", |
@@ -21,2 +21,4 @@ # @mrtujiawei/utils # | ||
- [Queue](#queue) | ||
- [Lock](#lock) | ||
- [DateTimeTool](#datetimetool) | ||
@@ -123,3 +125,3 @@ <!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
```javascript | ||
import { Queue, } from '@mrtujiawei/utils'; | ||
import { Queue } from '@mrtujiawei/utils'; | ||
@@ -143,1 +145,52 @@ const queue = new Queue(); | ||
``` | ||
### Lock ### | ||
异步流程加锁 | ||
```javascript | ||
import { Lock, sleep } from '@/mrtujiawei/utils'; | ||
const lock = new Lock(1); | ||
/** | ||
* 异步任务只有等上次任务结束后才会开始执行下一个异步任务 | ||
*/ | ||
const run = async (value, timeout) => { | ||
try { | ||
await lock.lock(); | ||
// 异步任务 | ||
await sleep(timeout); | ||
console.log(value); | ||
} finally { | ||
lock.unLock(); | ||
} | ||
}; | ||
run(0, 1000); | ||
run(1, 100); | ||
run(2, 0); | ||
output: 0 1 2 | ||
``` | ||
<!-- | ||
### TaskQueue ### | ||
--> | ||
<!-- | ||
### ResponsibilityChain ### | ||
--> | ||
### DateTimeTool ### | ||
日期时间处理类 | ||
解析时间太复杂,没做 | ||
```javascript | ||
import { DateTimeTool } from '@/mrtujiawei/utils'; | ||
DateTimeTool.timeFormat(new Date(), ':'); // hh:mm:ss | ||
DateTimeTool.dateFormat(new Date(), '-'); // yyyy-mm-dd | ||
``` |
220644
194