(中间值).then 不是函数

(intermediate value).then is not a function

我是 Javascript 的新手。 由于一些遗留系统,目前我正在将一些 ES6 代码转换回 ES5 代码。 我转换了以下代码:

$row.find('.gridCellDetailAction')
 .each((i, elem) => $translate('Grid.Show' + $(elem).attr('data-title') + 's')
.then(trans => $(elem).attr('title', trans)));

对此

   $row.find('.gridCellDetailAction')
        .each(function (i, elem) {
              $translate('Grid.Show' + $(elem).attr('data-title') + 's')
   }.then(function (trans) { $(elem).attr('title', trans) }));

现在我收到以下错误:

(intermediate value).then is not a function

现在我知道我在 then 上做错了什么。但是我怎样才能得到相同的结果呢?

错在哪里:
从您的代码中,我将删除 $row.find('.gridCellDetailAction').each();

function (i, elem) {
    $translate('Grid.Show' + $(elem).attr('data-title') + 's')
}
.then(function (trans) { $(elem).attr('title', trans) })

这段代码似乎有误; 所以,我们需要把 .then(function (trans) { $(elem).attr('title', trans) }) 放在 function()


完整代码:

$row.find('.gridCellDetailAction')
    .each(function (i, elem) {
        $translate('Grid.Show' + $(elem).attr('data-title') + 's')
            .then(function (trans) { $(elem).attr('title', trans) });
    });

通过一些格式设置,您可以轻松看出差异。 一次你在 $translate 的结果上调用 then,另一次你在函数定义上调用 then

$row.find('.gridCellDetailAction')
.each(
  (i, elem) => {
    $translate(
     'Grid.Show' + $(elem).attr('data-title') + 's'
    )
    .then(
      trans => $(elem).attr('title', trans)
    )
  }
);
$row.find('.gridCellDetailAction')
.each(
  function (i, elem) {
    $translate('Grid.Show' + $(elem).attr('data-title') + 's')
  }// <-- The error is here
  .then(
    function (trans) { 
      $(elem).attr('title', trans) 
    }
  )
);

这是正确的:

$row.find('.gridCellDetailAction')
.each(
  function (i, elem) {
    $translate('Grid.Show' + $(elem).attr('data-title') + 's')
    .then(  //Now then is called on the result of $translate
      function (trans) { 
        $(elem).attr('title', trans) 
      }
    )
  }
);