Commit f2558f6

Anton Medvedev <anton@medv.io>
2022-06-08 22:36:22
Code style
1 parent 6d614b7
src/experimental.ts
@@ -21,7 +21,7 @@ export async function retry<T>(count: number, callback: () => T): Promise<T>
 export async function retry<T>(
   count: number,
   duration: Duration,
-  callback?: () => T
+  callback: () => T
 ): Promise<T>
 export async function retry<T>(
   count: number,
@@ -29,18 +29,19 @@ export async function retry<T>(
   b?: () => T
 ): Promise<T> {
   const total = count
-  let cb: () => T
+  let callback: () => T
   let delay = 0
   if (typeof a == 'function') {
-    cb = a
+    callback = a
   } else {
     delay = parseDuration(a)
     assert(b)
-    cb = b
+    callback = b
   }
+  let lastErr: unknown
   while (count-- > 0) {
     try {
-      return await cb()
+      return await callback()
     } catch (err) {
       if ($.verbose) {
         console.error(
@@ -49,11 +50,12 @@ export async function retry<T>(
             (delay > 0 ? `; next in ${delay}ms` : '')
         )
       }
-      if (count == 0) throw err
+      lastErr = err
+      if (count == 0) break
       if (delay) await sleep(delay)
     }
   }
-  throw 'unreachable'
+  throw lastErr
 }
 
 export async function spinner<T>(callback: () => T): Promise<T>
@@ -62,9 +64,7 @@ export async function spinner<T>(
   title: string | (() => T),
   callback?: () => T
 ): Promise<T> {
-  if (typeof title == 'string') {
-    assert(callback)
-  } else {
+  if (typeof title == 'function') {
     callback = title
     title = ''
   }
test/experimental.test.js
@@ -24,11 +24,11 @@ function zx(script) {
   return $`node build/cli.js --experimental --eval ${script}`
 }
 
-test('retry works', async () => {
+test('retry() works', async () => {
   const now = Date.now()
   let p = await zx(`
     try {
-      await retry(5, 50, () => $\`exit 123\`)
+      await retry(5, '50ms', () => $\`exit 123\`)
     } catch (e) {
       echo('exitCode:', e.exitCode)
     }
@@ -55,7 +55,7 @@ test('spinner() works', async () => {
 
 test('spinner() with title works', async () => {
   let out = await zx(`
-    await spinner('processing', async () => sleep(100))
+    await spinner('processing', () => sleep(100))
   `)
   assert.match(out.stderr, 'processing')
 })
tsconfig.json
@@ -5,8 +5,6 @@
     "moduleResolution": "nodenext",
     "module": "nodenext",
     "strict": true,
-    "noImplicitReturns": true,
-    "noFallthroughCasesInSwitch": true,
     "outDir": "./build",
     "declaration": true
   },