在then回调函数中使用await - 关键字'await'是保留字。

18
在node.js中,我有一个数据库事务,在其中我想在then回调函数中调用一个async方法,但我收到了错误消息the keyword 'await' is reserved
这是异步的saveImage函数:
const saveImage = async (parsedLink) => {
  AWS.config.region = config.awsSettings.region;
  AWS.config.accessKeyId = config.awsSettings.accessKeyId;
  AWS.config.secretAccessKey = config.awsSettings.secretAccessKey;
  const bucket = new AWS.S3({
    params: {
      Bucket: config.awsSettings.images_bucket_name,
    },
  });

  const currentDateString = new Date().toISOString().replace(/\:|\./g, '-');
  const bodystream = new Buffer(parsedLink.imgUrl, 'binary');
  const imageUrlDomain = parseDomain(parsedLink.linkUrl).domain;

  const params = {
    Key: `${parsedLink.id}/${imageUrlDomain}_${currentDateString}${parsedLink.imgType}`,
    ContentType: parsedLink.imageMime,
    ContentEncoding: 'base64',
    Body: bodystream,
  };

  const resultPromise = await bucket.upload(params).promise();
  return resultPromise.Location;
};

如果我想使用saveImage函数,我会收到错误消息。

module.exports.addTestObject = async (ctx) => {
  const testObj = ctx.request.body;
  try {
    switch (testObj.type) {
      case interestT.getType.link: {
        const knexTestObject = TestObject.knex();
        transaction(knexTestObject, trx =>
            TestObject.query(trx)
              .insert({
                interestDate: testObj.date,
              })
              .then(newInterest => {
                // save image
                if (parsedLink.saveImg) {
                  parsedLink.imgUrl = await saveImage(testObj);
                }

                newInterest.$relatedQuery('linkInterestsRel', trx).insert({
                  linkHeader: testObj.linkHeader,
                }),
              }
              ),
          )
          .then((linkInterest) => {
            console.log(linkInterest);
          })
          .catch((err) => {
            throw err;
          });
        break;
      }
      default:
        break;
    }
    ctx.response.body = interestObj;
  } catch (err) {
    const statusCode = err.status || 400;
    ctx.throw(statusCode, err.message);
  }
};

4
您只能在async函数中使用await关键字,而您的.then()回调函数不是async函数。 - Pointy
5
.then(async (newInterest) => { -> .then(async (新兴趣) => { - Jonas Wilms
@JonasW. 谢谢,你能把它写成一个答案吗?这样我就可以关闭这个问题了。 - Matt
不要在 async 函数内使用 then 回调。 - Bergi
1个回答

54

普通的function会同步执行直到返回。因此,您不能在其中使用await,因为您无法以同步方式等待异步事件。

JavaScript还有async function,它们看起来像普通函数,但概念上非常不同:它们运行同步,直到遇到await,然后停止并在承诺解决后继续。因此,它们无法同步返回结果,而是返回一个Promise,当函数完成执行时,则解决该Promise。

因此,您需要将您的函数转换为async函数:

 async function getUsername() { // <-- async keyword here
    return (await getUser()).name; // <-- await can be used inside
 }

现在这也适用于 .then 回调函数内:

 getUser().then(async function(user) {
    const friends = await getFriends(user);
    // ...
 })

但这有点混淆了抽象的异步函数和它们底层的原始Promise。如果你只是使用await关键字等待Promise而不是添加一个.then回调函数,代码会更易读:

 (async function() {
    const user    = await getUser();
    const friends = await getFriends(user);
 })();

具体问题可以重新表述为:


 const linkInterest = await transaction(knexTestObject, async trx => {
     const newInterest = await TestObject.query(trx)
          .insert({  interestDate: testObj.date,   });

     if (parsedLink.saveImg) {
       parsedLink.imgUrl = await saveImage(testObj);
     }

    await newInterest.$relatedQuery('linkInterestsRel', trx)
       .insert({  linkHeader: testObj.linkHeader, }),
});

1
如果想要使用await来查询(实际上应该这样做),则transaction回调仍然需要是一个async function - Bergi
我看过的最清晰的关于如何使用await代替.then的解释。 - brt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接