【问题标题】:Can't execute code within .then callback function?无法在 .then 回调函数中执行代码?
【发布时间】:2019-07-15 14:11:36
【问题描述】:

如何在下面代码的最后一条注释处执行代码?出于某种原因,我不允许这样做。我的评论不是在回调函数里面吗?

代码是 Stackoverflow 上几个答案的结果,我不太明白发生了什么。

browser.browserAction.onClicked.addListener(async tab => {

    const contentScriptReady = Promise.all([
        browser.tabs.executeScript(tab.id, {file: "axios.min.js"}),
        browser.tabs.executeScript(tab.id, {file: "content.js"}),
        browser.tabs.executeScript(tab.id, { file: "sweetalert2.all.min.js" }),
        browser.tabs.insertCSS(tab.id, { file: "styles.css" })
    ]);

    const connectionStatus = {};

    async function getConnectionStatusData(logicalAddress) {

        let cooperations = await axios.get('http://api.ntjp.se/coop/api/v1/cooperations.json', {
        params: {
          connectionPointId: connectionPointId,
          logicalAddressId: logicalAddressId,
          serviceDomainId: serviceDomainId,
          serviceConsumerId: serviceConsumerId,
          include: "serviceContract"
         }
        });

        /* some more let x = await axios.get... */

        connectionStatus.supportedServiceContracts = await Promise.all( cooperations.data.map(cooperation => axios.get('http://api.ntjp.se/coop/api/v1/serviceProducers.json', {
          params: {
            connectionPointId,
            logicalAddressId,
            serviceDomainId,
            serviceConsumerId,
            serviceContractId: cooperation.serviceContract.id,
          },
           }).then(response => ({ // I want to process the response but I can't put executable code here
            serviceContract: cooperation.serviceContract.namespace,
            serviceProducerDescription: response.data[0].description,
            serviceProducerHSAId: response.data[0].hsaId,
            }))
          )
        );

        await contentScriptReady;
        browser.tabs.sendMessage(tab.id, connectionStatus);

    }

});

【问题讨论】:

  • 你遇到什么错误
  • 当我尝试将任何可执行代码放在那里时,我的 IDE 就会开始抱怨,但如果我把 console.log("test"); 放在那里,我会得到 Uncaught SyntaxError: Unexpected token .。它指的是时期。请注意,问题出在单行注释上,而不是在中间的注释块上。
  • 您的代码中究竟在哪里得到了错误。
  • 我没有收到任何错误,但如果我尝试将可执行代码放在单行注释所在的位置,我确实会收到错误。
  • 你在哪里打电话getConnectionStatusData?您正在等待在函数声明之外创建的 contentScriptReady 是可疑的,请注意未处理的拒绝。

标签: javascript promise callback axios


【解决方案1】:

这是object literal returned from an arrow function。你不能在那里发表声明。您将需要将其重写为

….then(response => {
    console.log("example");  // executable code here
    return {
        serviceContract: cooperation.serviceContract.namespace,
        serviceProducerDescription: response.data[0].description,
        serviceProducerHSAId: response.data[0].hsaId,
    };
})

【讨论】:

  • 非常感谢!我阅读了您链接到的问题的答案,现在我明白了。欣赏!
  • 顺便说一句,是不是因为没有它会是双花括号?
  • 我的错!那么只有当箭头函数返回一个object时,它必须用括号括起来?如果是这样,那是因为没有它会是双花括号吗?
  • 是的,具有简洁主体的箭头函数可以返回任何类型的表达式,但对象文字除外,这会被误认为是块体。即使使用双花括号,它也是无效的 - 对象文字不能用作语句。
  • 再次感谢您的解释!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 2019-08-01
  • 2019-10-08
  • 1970-01-01
  • 2020-06-02
  • 1970-01-01
相关资源
最近更新 更多