Wraps a promise and returns a tuple with either an error or the result. Optionally executes a finally callback after the promise settles.
import { to } from "@mrspartak/promises"import { api } from "./api"// Simple tuple destructuringconst [apiError, user] = await to(api.get("/me"))if (apiError) { // Handle error}// Using finally$component.isLoading = trueconst [apiError, status] = await to(api.post("/me/status", { status: "online" }), () => { $component.isLoading = false})if (apiError) { // Handle error} Copy
import { to } from "@mrspartak/promises"import { api } from "./api"// Simple tuple destructuringconst [apiError, user] = await to(api.get("/me"))if (apiError) { // Handle error}// Using finally$component.isLoading = trueconst [apiError, status] = await to(api.post("/me/status", { status: "online" }), () => { $component.isLoading = false})if (apiError) { // Handle error}
The type of the value the input promise resolves to.
The input promise to be wrapped.
Optional
Optional finally callback to be executed after the promise settles.
A promise that resolves to a tuple containing either an error or the result.
Wraps a promise and returns a tuple with either an error or the result. Optionally executes a finally callback after the promise settles.
Example