【问题标题】:then is not a functionthen 不是函数
【发布时间】:2016-10-15 13:44:44
【问题描述】:

我在我的 node.js 项目中运行以下代码。我需要“imagemin”和“imageminGifsicle”。我收到以下错误

}).then(文件 => { ^ TypeError: imagemin(...).then 不是函数

我以前没有在我的项目中使用过 Promise。我是否需要包含它们才能使用 .then ?

imagemin(['export/sample/out.gif'], 'export/sample/out2.gif', {
 plugins: [
  imageminGifsicle({optimizationLevel: 1})
 ]
}).then(files => {
  console.log(files);
  console.log("finished");
}).catch(err => {
  console.log("ERR:"+err);
  throw err;
});

【问题讨论】:

  • 你是如何安装 imagemin 的?它依赖于“promise.pipe”并且应该支持基于其文档的 .then 链接。

标签: javascript node.js promise imagemin


【解决方案1】:

.then 与 Promise 相关,imagemin added this only on 5.0.0

你用的是什么版本的?

【讨论】:

    【解决方案2】:

    嗯,是的——你可以为 promises 选择你喜欢的库,然后使用带有 promises 的“then”函数。在这种情况下,您使用的是 imagemin:

    const imagemin = require('imagemin');
    const imageminMozjpeg = require('imagemin-mozjpeg');
    const imageminPngquant = require('imagemin-pngquant');
    

    imagemin on GitHub

    关于使用 Promises,您可以阅读 MDNhere on stackoverflow 上的一些规范和信息

    【讨论】:

      猜你喜欢
      • 2020-03-14
      • 2019-02-10
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 2021-07-24
      • 2020-09-06
      • 1970-01-01
      相关资源
      最近更新 更多