Commit 4fdd786

庄天翼 <zty0826@gmail.com>
2021-05-13 15:55:15
Add a builtin util function sleep (#70)
* Add sleep * remove semicolon Co-authored-by: Anton Medvedev <antonmedv@google.com>
1 parent 9a1a743
index.d.ts
@@ -27,6 +27,8 @@ export function cd(path: string)
 
 type QuestionOptions = { choices: string[] }
 
+export function sleep(ms: number): Promise<void>
+
 export function question(query?: string, options?: QuestionOptions): Promise<string>
 
 export class ProcessOutput {
index.mjs
@@ -16,6 +16,7 @@ import {existsSync} from 'fs'
 import {exec} from 'child_process'
 import {createInterface} from 'readline'
 import {default as nodeFetch} from 'node-fetch'
+import {promisify} from 'util'
 import which from 'which'
 import chalk from 'chalk'
 import shq from 'shq'
@@ -122,6 +123,8 @@ export async function fetch(url, init) {
   return nodeFetch(url, init)
 }
 
+export const sleep = promisify(setTimeout)
+
 export class ProcessOutput {
   #code = 0
   #stdout = ''
README.md
@@ -136,7 +136,19 @@ let token = await question('Choose env variable: ', {
 })
 ```
 
+### `sleep()`
 
+This is a wrapper around setTimeout.
+
+```ts
+function sleep(ms: number): Promise<void>
+```
+
+Usage:
+
+```js
+await sleep(1000)
+```
 
 ### `chalk` package
 
zx.mjs
@@ -19,7 +19,7 @@ import os, {tmpdir} from 'os'
 import {promises as fs} from 'fs'
 import url from 'url'
 import {v4 as uuid} from 'uuid'
-import {$, cd, question, fetch, chalk, ProcessOutput} from './index.mjs'
+import {$, cd, question, fetch, chalk, sleep, ProcessOutput} from './index.mjs'
 import {version} from './version.js'
 
 Object.assign(global, {
@@ -28,6 +28,7 @@ Object.assign(global, {
   fetch,
   question,
   chalk,
+  sleep,
   fs,
   os,
 })