函数包装器返回函数而不是实际对象

Function wrapper returning function instead of actual object

我试图围绕一个异步函数创建一个包装器,当我调用该函数时,我总是得到一个函数而不是我想要得到的对象。

function wrap<T extends Function>(fn: T){
    return <any> async function (...args) {
        let result = await fn(...args)
        return {
            statusCode: 200,
            body: JSON.stringify({
                data: result

            }),
        }
    };
}

let main = async (test: string) => {
    console.log(`calling api  ${test}`);
    return wrap(() => {
        console.log("")
        return {
            foo: "bar",
        }
    });
};

main("test").then((res)=>{
    console.log(res)
    console.log(typeof res)  // <-- function 
})

我错过了什么?
任何帮助将不胜感激。

您没有调用该函数,您只是将它括在括号中。我相信这就是你所需要的。

function wrap<T extends Function>(fn: T){
    return <any> async function (...args) {
        let result = await fn(...args)
        return {
            statusCode: 200,
            body: JSON.stringify({
                data: result

            }),
        }
    };
}

let main = async (test: any) => {
    console.log(`calling api  ${test}`);
    return wrap(() => {
        console.log("")
        return {
            foo: "bar",
        }
    })();
};

main("test").then((res)=>{
    console.log(res)
    console.log(typeof res)  // <-- object
})