Skip to content

A simple to use (async await) wrapper to resolve functions, promises and more. It provides a timeout functionalitiy to delay/timeout execution and always returns an array of type [error, result] to ease error handling and eliminate the need to (try-)catch.

License

Notifications You must be signed in to change notification settings

kossidts/await-resolver

Repository files navigation

Await Resolver

License NPM Package Version GitHub top language Size Last Commit Workflow CI

An easy-to-use async/await wrapper to resolve functions, promises, and any asynchronous task.

await-resolver always returns an array of type [error, result] ensuring consistent error handling across your code. You no longer have to use try/catch blocks or callbacks if you don't want to.

Additionally await-resolver provides a timeout functionality to delay/timeout an execution. You can use it as a sleep function.

Syntax:

await resolver(something)

await resolver(something, timeout)

await resolver(fn, timeout, fn_param1, fn_param2, ..., fn_paramN)

await resolver.sleep(timeout)

Or as a promise

resolver(something).then(([result, error]) => { ... })

resolver(something, timeout).then(([result, error]) => { ... })

resolver(fn, timeout, fn_param1, fn_param2, ..., fn_paramN).then(([result, error]) => { ... })

resolver.sleep(timeout).then(([result, error]) => { ... })

Installation

$ npm install await-resolver

Usage

Require CommonJS (default)
const resolver = require("await-resolver");
Import ES-Module (default)
import resolver from "await-resolver";
Import ES-Module (named)
import { resolver } from "await-resolver";

Await the resolver

// If you environment does not support top-level await,
// wrap everything in an async function.

// await-resolve a fetch result
const fetchRequest = fetch(urlOrRequest[, options]);

// in one go
let [error, data] = await resolver(fetchRequest.then(res => res.json()));

// or step by step
let [responseError, response] = await resolver(fetchRequest);
if(responseError){
    // return early
}
let [dataError, data] = await resolver(response.json());



// await-resolve anything: a function, a promise, an error, or any kind of value
let [error, result] = await resolver(anything);

// Use timeout to delay execution
let timeout = 1500; // ms just like setTimeout

// just sleep
await resolver(null, timeout);

// Resolve a function without arguments, with timeout
let [error, result] = await resolver(my_function, timeout);

// Resolve a function with arguments, without timeout
let multiply = (a, b) => a * b;
let [error, result] = await resolver(multiply, null, 4, 5); // => [null, 20]

// Resolve a function with arguments, with timeout
let [error, result] = await resolver(multiply, timeout, 4, 5); // => [null, 20] after timeout

Or use the resolver as a regular promise

resolver(anything).then(([error, result]) => {
    if (error) {
        // do something
    } else {
        // do something else
    }
});

// sleep
resolver(null, timeout).then(([error, result]) => {
    // do something
});

let multiply = (a, b) => a * b;

resolver(multiply, timeout, 4, 5).then(([error, result]) => {
    // do something
});

License

See LICENSE.

Copyright

Copyright © 2022. Kossi D. T. Saka.

About

A simple to use (async await) wrapper to resolve functions, promises and more. It provides a timeout functionalitiy to delay/timeout execution and always returns an array of type [error, result] to ease error handling and eliminate the need to (try-)catch.

Resources

License

Stars

Watchers

Forks

Packages

No packages published