无服务器授权方异步等待不起作用

Serverless authorizer async await not working

我的无服务器应用程序一直在使用 Node 6,我决定迁移到 async/await,因为版本 8.x 已发布。

尽管如此,我在授权函数方面遇到了问题。由于我删除了回调参数并只返回值,它停止工作了。如果我向回调参数发送一些内容,它会继续正常工作,但它不像 async/await。即使我抛出异常也无法正常工作。

module.exports.handler = async (event, context) => {

    if (typeof event.authorizationToken === 'undefined') {
        throw new InternalServerError('Unauthorized');
    }

    const decodedToken = getDecodedToken(event.authorizationToken);
    const isTokenValid = await validateToken(decodedToken);

    if (!isTokenValid) {
        throw new InternalServerError('Unauthorized');
    } else {
        return generatePolicy(decodedToken);
    }
};

关于如何进行的任何建议?

谢谢大家!

我在这里遇到了同样的问题。授权者似乎还不支持 async/await。一种解决方案是获取整个 async/await 函数并在处理程序内部调用。像这样:

const auth = async event => {
    if (typeof event.authorizationToken === 'undefined') {
        throw new InternalServerError('Unauthorized');
    }

    const decodedToken = getDecodedToken(event.authorizationToken);
    const isTokenValid = await validateToken(decodedToken);

    if (!isTokenValid) {
        throw new InternalServerError('Unauthorized');
    } else {
        return generatePolicy(decodedToken);
    }
}
module.exports.handler = (event, context, cb) => {
    auth(event)
      .then(result => {
         cb(null, result);
      })
      .catch(err => {
         cb(err);
      })
};

对于 2020 年到达这里的人们 - 现在它像 OP 描述的那样工作。