首页 > 解决方案 > 仅当 'module' 选项设置为 'esnext' 时才允许使用顶级 'await' 表达式

问题描述

我正在执行 Stripes 集成步骤,并在步骤 2.1 中发现我的代码错误(https://stripe.com/docs/connect/collect-then-transfer-guide#create-an-account-link

如何修复此错误?

代码:

const stripe = require('stripe')('someID');
const account = await stripe.accounts.create({
  type: 'express',
});

错误:

仅当 'module' 选项设置为 'esnext' 或 'system' 并且 'target' 选项设置为 'es2017' 或更高版本时才允许使用顶级 'await' 表达式。ts(1378)

标签: javascriptnode.jsstripe-paymentsnode-modules

解决方案


您可以将 const 帐户的代码包装在异步函数中,因为您的目标选项不支持顶级等待。

 const account = async () => {
        await stripe.accounts.create({
          type: "express",
  });
};

这取决于您的代码是要返回某些内容还是要在等待之后执行一些其他任务。

如果您想使用顶级等待,有关使用顶级等待的更多信息请参见https://stackoverflow.com/a/56590390/9423152


推荐阅读