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

如何解决JavaScript中follow(...).then(...).done不是函数的错误

解决 Uncaught TypeError: follow(...).then(...).done is not a function 错误

嘿,我刚搞定了一模一样的错误!分享下我的解决思路,应该能帮到你——我之前也盯着这个报错折腾了好久,涉及到app.js里调用follow()的代码块,还有follow.js的函数实现,最后参考@sorja的方案调整后终于解决了。

错误根源分析

这个报错的核心问题其实很清晰:

  • 要么你的follow()函数没有返回标准的Promise对象,导致调用then()之后没法继续链式调用(毕竟原生Promise根本没有done()这个方法);
  • 要么你混淆了原生Promise和jQuery Deferred的API——done()是jQuery异步对象的方法,原生Promise只认then()和catch()。

具体调整方案

第一步:修改follow.js,让函数返回Promise

把follow()函数改成明确返回原生Promise的结构,包裹你的异步逻辑(比如API请求):

// follow.js 调整后的代码
function follow(userId) {
  // 用Promise包裹所有异步操作
  return new Promise((resolve, reject) => {
    // 这里替换成你实际的异步逻辑,比如fetch请求或者AJAX
    fetch(`/api/follow/${userId}`, { method: 'POST' })
      .then(response => {
        if (!response.ok) throw new Error('关注请求失败');
        return response.json();
      })
      .then(result => resolve(result)) // 成功时传递结果
      .catch(error => reject(error)); // 失败时传递错误
  });
}

第二步:调整app.js里的调用代码

把原来的done()换成原生Promise的then()和catch(),保持链式调用的一致性:

// app.js 调整后的调用代码
follow(targetUserId)
  .then(successData => {
    // 处理关注成功的逻辑
    console.log('关注成功!', successData);
    // 比如更新页面UI、提示用户等
  })
  .catch(error => {
    // 处理关注失败的逻辑
    console.error('关注失败:', error);
    // 比如显示错误提示
  });

如果你需要兼容jQuery Deferred

如果你的项目还在依赖jQuery的异步逻辑,也可以让follow()返回jQuery的Deferred对象,这样就能继续用done():

// 兼容jQuery版本的follow.js
function follow(userId) {
  const deferred = $.Deferred();
  $.post(`/api/follow/${userId}`)
    .done(response => deferred.resolve(response))
    .fail(error => deferred.reject(error));
  return deferred.promise();
}

// 调用时可以保留done()
follow(targetUserId)
  .done(data => console.log('关注成功'))
  .fail(err => console.error('关注出错'));

最终效果

我按照第一种方案调整后,那个烦人的类型错误直接消失了,异步逻辑也能正常执行。核心就是确保follow()返回一个可链式调用的异步对象,并且调用的方法和对象类型匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:03:11