Skip to content

async/await usage #23

@thebleucheese

Description

@thebleucheese

Just in case anyone's using this and trying to run in modern Node, the callbacks aren't standard so promisify requires some work to get it behaving.

Not perfect but it's a start if you're trying to reuse connect style middleware with direct calls in something like a cron.

    require('run-middleware')(server);

    server.runMiddleware[util.promisify.custom] = (path, options) => {
        return new Promise((resolve, reject) => {
            server.runMiddleware(path, options, function (code,data,headers) {
                let payload = {code: code, data: data, headers: headers};
                resolve(payload);
            });
        });
    };

    const runMiddlewarePromise = util.promisify(server.runMiddleware);

    const runMiddlewareAsync = async (path, options) => {
        try {
            const middlewareRes = await runMiddlewarePromise(path, options);
            return middlewareRes;
        } catch(e) {
            console.error("NOT IMPLEMENTED: ", e);
            return false;
        }
    };

    // ======= Execute with the following ==============
    let resTest = await runMiddlewareAsync('/my/url',{
                    secure: true,
                    connection: {},
                    method:'GET'                    
                });

    console.log("res Test: ", resTest);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions