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

NodeJS中await无作用报错排查:Vimeo视频上传API开发问题

Vimeo上传API await无效及数据未返回问题解决方案
  • 问题根源:client.upload是基于回调的异步API,并非返回Promise对象,所以await无法等待其执行完成,函数会直接返回未赋值link的data,导致后续逻辑崩溃。
  • 解决思路:将回调式的API包装为Promise,利用async/await等待异步流程全部完成后再返回数据。

修改后的代码:

const videoUpload = async (data) => {
  const file_name = data.path;

  // 封装upload为Promise
  await new Promise((resolve, reject) => {
    client.upload(
      file_name,
      {
        name: "Untitled",
        description: "The description goes here.",
      },
      (uri) => {
        // 封装request为Promise
        new Promise((reqResolve, reqReject) => {
          client.request(uri + "?fields=link", function (error, body, statusCode, headers) {
            if (error) {
              console.log("There was an error making the request.");
              console.log("Server reported: " + error);
              reqReject(error);
              return;
            }
            data.link = body.link;
            reqResolve();
          });
        }).then(resolve).catch(reject);
      },
      (bytes_uploaded, bytes_total) => {
        const percentage = ((bytes_uploaded / bytes_total) * 100).toFixed(2);
        console.log(bytes_uploaded, bytes_total, percentage + "%");
      },
      (error) => {
        console.log("Failed because: " + error);
        reject(error);
      }
    );
  });

  return data;
};
  • 额外说明:如果Vimeo SDK支持Promise化调用(部分SDK会提供promisify方法或原生Promise版本),也可以直接使用对应方法简化代码,核心是确保await等待的是一个Promise对象,保证异步操作完成后再返回数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 15:40:21