函数在Promise.all(reduce(acc,item)=> {}))函数内部丢失上下文

时间:2019-06-21 00:09:29

标签: javascript amazon-web-services promise reduce

我有一个功能

const integration = async (event) => {
    const user = await authenticateUser({
        email,
        password,
    })
    const authentication = path(['idToken', 'jwtToken'], user)

    return Promise.all(reduce(async (prevEvent, endpointId) => {
        const endpointDesc = endpoints[endpointId]

        let apiEvent = endpointDesc.actionEvent
        apiEvent.authentication = authentication

        if (endpointDesc.parameters.length > 0) {
            apiEvent = reduce((eventAcc, param) => {
                const prevPayload = eventAcc.payload
                const newPayload = { prevPayload, [param]: prevEvent.body[param] }
                return { ...apiEvent, payload: newPayload }
            }, apiEvent, apiEvent.parameters)
        }

        const lambdaParams = {
            FunctionName: apiFunctionArn,
            Payload: apiEvent,
        }

        const lambdaRes = await lambda.invoke(lambdaParams).promise()
        return lambdaRes
    }, Promise.resolve(), event.endpointId))
}

错误是undefined is not a function

lambda.invoke是导致错误的代码段

lambda对象来自const lambda = new AWS.Lambda()aws-sdk

lambda.invoke,在与return Promise.all(reduce(async (prevEvent, endpointId) => {匹配的行之前,应该起作用

我已经尝试过lambda.invoke.bind(lambda)之类的东西,但是老实说我最后一次完全忘记了面向对象的javascript:X

如果这很有意义,我使用reduce中的ramda

感谢前进!

0 个答案:

没有答案