You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS动态import加载函数跨组件调用返回空白输出异常排查

问题原因

你现在的代码有两个核心问题,直接导致外部调用拿不到结果:

  • javascriptRunner 外层函数本身没有写return语句,默认返回undefined
  • 你写在.then()回调里的return f.default(...input),只会把结果返回给Promise内部的链式调用,不会传递到外层函数的调用结果里
    你在当前模块能看到正确输出,只是回调里的console.log正常打印了日志,本质上函数对外没有返回任何有效值,所以其他组件导入调用时只能拿到空白的undefined。
    另外你现在连续两次执行f.default(...input),如果用户提交的代码带副作用(比如依赖随机数、修改全局状态、计数类逻辑),两次执行结果可能不一致,会导致判题错误。
修复代码

直接把动态import生成的Promise对象返回给外层调用方即可,同时把执行结果缓存避免重复运行用户代码:

const javascriptRunner = (fileLocation, input) => {
  //judge functionality
  return import(`../codes/${fileLocation}`).then((f) => {
    const output = f.default(...input)
    console.log(
      'output:',
      output,
      'type:',
      typeof output,
      'for input :',
      input
    )
    return output
  })
}
调用注意事项

动态import()是异步API,修复后的javascriptRunner返回值是Promise,外部组件调用时必须用异步写法拿结果,不能同步取值:

// 推荐写法:在async函数里用await拿结果
const runResult = await javascriptRunner('target-file.js', [testCaseParam1, testCaseParam2])
// 后续用runResult和预期输出做比对判题即可

// 也可以用.then写法
javascriptRunner('target-file.js', [testCaseParam1, testCaseParam2]).then(runResult => {
  // 判题逻辑写在这里
})

内容的提问来源于stack exchange,提问作者Zoheb Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 18:54:24