Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use a custom class extends Promise #12661

Closed
renyaoxiang opened this issue Dec 5, 2016 · 4 comments
Closed

how to use a custom class extends Promise #12661

renyaoxiang opened this issue Dec 5, 2016 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@renyaoxiang
Copy link

renyaoxiang commented Dec 5, 2016

i write a new class extends promise ,but it does not work in my browser.
i am not sure if it is caused by a bug of typescript or my inaccurate usage。

while i make a new instance like this:
this.mypromise = new PPromise()

the browse throw an error
“ Uncaught TypeError: # is not a promise”

the PPromise defination is:

export class PPromise<T> extends Promise<T>{
    resolve: (value?: T | PromiseLike<T>) => void
    reject: (value?: T | PromiseLike<T>) => void
    constructor(callback?:
        ( resolve: (value?: T | PromiseLike<T>) => void,
          reject: (value?: T | PromiseLike<T>) => void
        ) => void) {
        super((resolve, reject) => {
          this.resolve = resolve
          this.reject = reject
          callback && callback(this.resolve, this.reject)
      })
  }
}
@HerringtonDarkholme
Copy link
Contributor

This is probably transpiler's limitation. See #12123 (comment) for more information.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Dec 16, 2016
@masterkidan
Copy link

@mhegazy : Tried using the Object.setPrototype here, but the error is actually thrown in the super.apply call,
Is there any workaround for this?

@jvanoostveen
Copy link

I have yet to get this to work, when in pure JavaScript (in Chrome) it works like a charm, in node, it also works, but when trying to do this in TypeScript (running in node with TSNode), I keep getting the is not a Promise error...

@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2017

Sorry, this was misslabled. this is is not the same as #12123. This is not the same as other ES5-builtins. this is a duplicate of #15202.

The main issue here is that ES6 classes (e.g. Promise) are not callable, the way ES5 built in classes (e.g. Array). The emitted code that the compiler produces uses super.call which throws in ES6 engine.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

6 participants