When I run this in a node repl, that's exactly what happens - perhaps your
simplified example has simplified out the bug?

On Sat, Jul 27, 2019 at 6:57 PM #!/JoePea <[email protected]> wrote:

> I feel like I'm going crazy, but I have a class hierarchy, and one of
> the constructors in the hierarchy defers some logic to a microtask,
>
> ```js
> // class Foo
> constructor() {
>   Promise.resolve().then(() => {
>     this.methodThatSubclassOverrides()
>   })
> }
>
> methodThatSubclassOverrides() {}
> ```
>
> and in the subclass there is
>
> ```js
> // class Bar extends Foo
> constructor() {
>   super()
>   this.foo = 123
> }
>
> methodThatSubclassOverrides() {
>   console.log(this.foo) // should be "123"
> }
> ```
>
> You'd think the output should be "123" because the deferred code will
> run after construction is complete.
>
> However, in my current project, the promise deferral seems to run
> before the construction returns to the Bar constructor, thus this.foo
> is not set, and calls `methodThatSubclassOverrides` before the Bar
> class has a chance to run `this.foo = 123`. So the result of the
> console.log is "undefined".
>
> It is totally weird. Do Promises ever resolve before a constructor
> stack finishes?
>
> I don't have a simple reproduction at the moment.
>
> - Joe
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to