在Promise中调用super()方法:'super'关键字在这里出乎意料?

f1n*_*1nn 12 inheritance scope super ecmascript-6 es6-promise

我试图save()从子实例调用super方法.

// ImageKeeper.js
'use strict';

module.exports = class ImageKeeper extends FileKeeper {
  constructor(keeperPath, options) {
    super(`/${keeperPath}`, options)
    this.keeperPath = keeperPath;
  }

  save(filePath) {
    return new Promise((resolve, reject) => {
      this
        .resolvePath(filePath)
        .then(function(fileData) {
          var filePath = fileData[0],
            mime = fileData[1];

          super.save(filePath, mime); // <-- GETTING ERROR HERE
        })
        .catch(function(err) {
          reject(err)
        })
    })
  }
}

// FileKeeper.js
'use strict';

module.exports = class FileKeeper {
  constructor(keeperPath, options) {
    this.storagePath = path.resolve(`${env.paths.storage}${keeperPath}`);
    this.options = options;
  }

  save(filePath, mime) {
    return Promise
      ...
  }
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

/src/filekeeper/imagekeeper.js:110
          super.save(filePath, mime);
          ^^^^^

SyntaxError: 'super' keyword unexpected here
Run Code Online (Sandbox Code Playgroud)

如果我super.save(filePath, mime);save()方法的开头移动,它可以工作.

我已经尝试将绑定上下文绑定到上限范围:

save(filePath) {
  return new Promise((resolve, reject) => {
    this
      .then((fileData) => { // <-- bind context to upper scope

        super.save(filePath, mime);
Run Code Online (Sandbox Code Playgroud)

但是我得到了:

Unhandled rejection SyntaxError: 'super' keyword unexpected here
    at processImmediate [as _immediateCallback] (timers.js:374:17)
From previous event:
    at /src/filekeeper/imagekeeper.js:106:10
Run Code Online (Sandbox Code Playgroud)

这个,但没有运气.

有任何想法吗?谢谢.

ENV

root@8d1024b233c3:/src# node -v
  v4.1.1

docker -v
  Docker version 1.8.2, build 0a8c2e3
Run Code Online (Sandbox Code Playgroud)

T.J*_*der 9

看起来你在V8的处理中发现了一个错误super; 我已经在这里报告了这个错误,他们已经将它分类为Type-BugPriority-Medium.这是在仔细研究之后,导致我发布这个问题,这个答案证实了我怀疑这是一个V8错误.

如果您使用箭头函数(不是function函数)按照"我已经尝试将绑定上下文绑定到上部范围"注释(主代码块使用的function函数不起作用),它应该正常工作.

在等待修复时,如果您将该逻辑放在方法中,它会起作用:

someAppropriateName(fileData) {
  var filePath = fileData[0],
    mime = fileData[1];

  super.save(filePath, mime);
}
Run Code Online (Sandbox Code Playgroud)

...并从promise回调中调用该方法:

save(filePath) {
  return new Promise((resolve, reject) => {
    this
      .resolvePath(filePath)
      .then(fileData => {                      // **
          this.someAppropriateName(fileData);  // **
      })                                       // **
      .catch(function(err) {
        reject(err)
      })
  })
}
Run Code Online (Sandbox Code Playgroud)

要么:

save(filePath) {
  return new Promise((resolve, reject) => {
    this
      .resolvePath(filePath)
      .then(this.someAppropriateName.bind(this)) // **
      .catch(function(err) {
        reject(err)
      })
  })
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为错误是相当模糊的:只有在方法内的另一个箭头函数内有箭头函数时,它才会出现,而最里面的箭头函数使用由外部箭头函数定义的变量或参数(使用方法本身的东西)很好).


其他一些说明:

  1. 如果FileKeepersave返回一个承诺,似乎ImageKeeper应该使用和链接关闭它.你的代码只是抛弃了调用的结果super.save(...).

  2. 当你发现自己写作时new Promise,总是停下来问自己,所讨论的代码是否真的是链的根源.非常,非常,经常不是(我怀疑它不在您的代码中).请记住,每个人都会then回报承诺,承诺的力量主要在于链条.如果你不需要,不要打破链条.