UNPKG

@liberquack/utils

Version:
34 lines 926 B
import { useState } from "haunted/lib/core.js"; import { inlineErr } from "../../../inline-error.js"; /** * * @param job Async function to be executed * @param cb Callback executed everytime the stats change */ export function useAwait(job, cb) { const [loading, setLoading] = useState(false); const [err, setErr] = useState(undefined); const [result, setResult] = useState(undefined); const run = (async (...args) => { if (!loading) { setLoading(true); setErr(undefined); const [result, err] = await inlineErr(job(...args)); setErr(err); setResult(result); setLoading(false); return result; } }); const awaitStats = { run, loading, result: result, err }; if (cb) { cb(awaitStats); } return awaitStats; } //# sourceMappingURL=use-await.js.map