(无服务器框架模块)在 return 语句之前等待承诺解决

(Serverless Framework Module) wait for promise to resolve before return statement

无服务器框架模块是否可以在 returning 之前等待承诺的 "resolve"?

我知道 promise 本身无法做到这一点,但不同的 frameworks/libraries(express, Jasmine, hapijs 等)通过定义何时 return 的方法解决了这个问题.我需要这样的东西:

let http = require('http'),
    Promise = require('bluebird');

let action = (done) => {
  return new Promise((resolve, reject) => {
    http
      .get('http://domain.com', resolve.bind({}, 'all good!'))
      .on('error', reject.bind({}, 'all wrong!'));
  })
  .then((response) => {
    console.log('Result', response);
    return done(response);  // <----------- I wan't to see this as the response
                            //              of the lambda function
  });
};

module.exports.run = (event, context, cb) => cb(null, action(done));

不,承诺不会那样做。不可能从未来读取,不想(不能)阻塞。您的操作仍然是异步的。

但是鉴于您的导出无论如何都需要回调,您可以简单地异步调用它:

module.exports.run = (event, context, cb) => {
    action().then(res => cb(null, res), err=>cb(err));
};

当然,如果你刚刚兑现承诺就更好了。