Node Permanently Moved

    await-resolver

    1.1.0 • Public • Published

    Await Resolver

    License NPM Package Version GitHub top language Size Last Commit

    A simple to use (async await) wrapper to resolve functions, promises and more.

    It provides a timeout functionalitiy to delay/timeout execution.

    The resolver always returns an array of type [error, result] to ease error handling and eliminate the need to (try-)catch.

    Syntax:

    await resolver(something)

    await resolver(something, timeout)

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

    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]) => { ... })

    Installation

    $ npm i 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 inside an async function

    async () => {
        let anything; // can be 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.

    Install

    npm i await-resolver

    DownloadsWeekly Downloads

    2

    Version

    1.1.0

    License

    MIT

    Unpacked Size

    13.6 kB

    Total Files

    9

    Last publish

    Collaborators

    • kdts