-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
35 lines (34 loc) · 1.08 KB
/
Copy pathrun.js
File metadata and controls
35 lines (34 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { spinner } from "./spinner.js";
/**
* Run an async function wrapped in a spinner.
* The spinner starts automatically, resolves to the function's return value
* on success, or rethrows on failure (after displaying an error state).
*
* This is a convenience wrapper around `spinner()` for one-shot async tasks.
* For tracking *multiple* concurrent tasks, use `tasks()` instead.
*
* @template T
* @param {string} label Label shown while the task runs.
* @param {() => Promise<T>} fn Async work to execute.
* @param {import("./spinner.js").SpinnerOptions} [options]
* @returns {Promise<T>}
*
* @example
* const data = await run("Fetching data", () => fetchData());
*
* @example
* // Custom spinner style
* await run("Building", build, { style: "line", color: "magenta" });
*/
export async function run(label, fn, options = {}) {
const spin = spinner(label, options);
spin.start();
try {
const result = await fn();
spin.success();
return result;
} catch (err) {
spin.error(err?.message ?? String(err));
throw err;
}
}