Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/index.ts>_try

A JavaScript extension package for building strong and modern applications.
Latest
function _try
Re-export
import { _try } from "https://deno.land/x/ayonli_jsext@v0.9.72/index.ts";

Invokes an async generator function and renders its yield value and result in an [err, val] tuple.

Examples

Example 1

import _try from "@ayonli/jsext/try";

const iter = _try(async function* () {
    // do something that may fail
});

for await (const [err, val] of iter) {
    if (err) {
        console.error("something went wrong:", err);
    } else {
        console.log("current value:", val);
    }
}

Type Parameters

optional
E = unknown
optional
T = any
optional
A extends any[] = any[]
optional
TReturn = any
optional
TNext = unknown

Parameters

fn: (...args: A) => AsyncGenerator<T, TReturn, TNext>
...args: A

Returns

AsyncGenerator<[E, T], [E, TReturn], TNext>

Invokes a generator function and renders its yield value and result in an [err, val] tuple.

Examples

Example 1

import _try from "@ayonli/jsext/try";

const iter = _try(function* () {
    // do something that may fail
});

for (const [err, val] of iter) {
    if (err) {
        console.error("something went wrong:", err);
    } else {
        console.log("current value:", val);
    }
}

Type Parameters

optional
E = unknown
optional
T = any
optional
A extends any[] = any[]
optional
TReturn = any
optional
TNext = unknown

Parameters

fn: (...args: A) => Generator<T, TReturn, TNext>
...args: A

Returns

Generator<[E, T], [E, TReturn], TNext>

Invokes an async function and renders its result in an [err, res] tuple.

Examples

Example 1

import _try from "@ayonli/jsext/try";
import axios from "axios";

let [err, res] = await _try(async () => {
    return await axios.get("https://example.org");
});

if (err) {
    res = (err as any)["response"];
}

Type Parameters

optional
E = unknown
optional
R = any
optional
A extends any[] = any[]

Parameters

fn: (...args: A) => Promise<R>
...args: A

Returns

Promise<[E, R]>

Invokes a function and renders its result in an [err, res] tuple.

Examples

Example 1

import _try from "@ayonli/jsext/try";

const [err, res] = _try(() => {
    // do something that may fail
});

Type Parameters

optional
E = unknown
optional
R = any
optional
A extends any[] = any[]

Parameters

fn: (...args: A) => R
...args: A

Resolves an async generator and renders its yield value and result in an [err, val] tuple.

Examples

Example 1

import _try from "@ayonli/jsext/try";

async function* gen() {
    // do something that may fail
}

for await (const [err, val] of _try(gen())) {
    if (err) {
        console.error("something went wrong:", err);
    } else {
        console.log("current value:", val);
    }
}

Type Parameters

optional
E = unknown
optional
T = any
optional
TReturn = any
optional
TNext = unknown

Parameters

gen: AsyncGenerator<T, TReturn, TNext>

Returns

AsyncGenerator<[E, T], [E, TReturn], TNext>

Resolves a generator and renders its yield value and result in an [err, val] tuple.

Examples

Example 1

import _try from "@ayonli/jsext/try";
import { range } from "@ayonli/jsext/number";

const iter = range(1, 10);

for (const [err, val] of _try(iter)) {
    if (err) {
        console.error("something went wrong:", err);
    } else {
        console.log("current value:", val);
    }
}

Type Parameters

optional
E = unknown
optional
T = any
optional
TReturn = any
optional
TNext = unknown

Parameters

gen: Generator<T, TReturn, TNext>

Returns

Generator<[E, T], [E, TReturn], TNext>

Resolves a promise and renders its result in an [err, res] tuple.

Examples

Example 1

import _try from "@ayonli/jsext/try";
import axios from "axios";

let [err, res] = await _try(axios.get("https://example.org"));

if (err) {
    res = (err as any)["response"];
}

Type Parameters

optional
E = unknown
optional
R = any

Parameters

job: PromiseLike<R>

Returns

Promise<[E, R]>