0
class MyPromise<T = void> extends Promise<T> {
  started = false
  resolve: (t: T) => void = () => {}
  reject: (e: any) => void = () => {}
  constructor() {
    console.log('constructor')
    let resolve: (t: T) => void = () => {}
    let reject: (e: any) => void = () => {}
    super((res, rej) => {
      resolve = res
      reject = rej
    })
    this.resolve = resolve
    this.reject = reject
  }
}

const thing = new MyPromise<string>()
thing.resolve('a')
thing.then(t => console.log('then', t))

Constructor is called twice and I get this error

constructor
constructor
[TestError] TypeError: Promise resolve or reject function is not callable

Running on node 14.17.1

david_adler
  • 9,690
  • 6
  • 57
  • 97
  • 1
    `then` returns a new promise based on the subclassed Promise that was constructed. So you get one log for `new MyPromise` and another when you use `.then()` – Nick Parsons Dec 22 '21 at 12:08
  • Not sure if you have seen this already, but it also explains what occurs when you try and subclass a promise: [Extend Javascript promise and resolve or reject it inside constructor](https://stackoverflow.com/a/48159603) – Nick Parsons Dec 22 '21 at 12:10
  • 1
    Nice seems similar! – david_adler Dec 22 '21 at 12:12

0 Answers0