+ 2
Why am i getting the previous state of an object even after changing its state through a function ? [Solved]
3 Answers
+ 4
because 'this' where you modify state property is not the 'this' of the 'promise' object ^^
to keep 'this' as same object inside the inner function than in the 'promise' constructor, use arrow functions:
this.resolve = arg => {
/* ... */
}
this.reject = arg => {
/* ... */
}
however, why making a fake 'promise' function rather than using the built-in 'Promise' constructor :o ?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
+ 2
yes, that's how it works...
if you use "strict mode", you would be alerted by an error, as 'this' default to null instead of window ;P
another workaround, if you don't want to use arrow function (or if you don't declare function in same scope on 'this' reside) would be to explicitly bind the target function (does not work with arrow functions) with 'this':
this.resolve = (function(arg) {
/* ... */
}).bind(this)
;)
0
visph ty , i just checked inside resolve function for "this == window" and it outputs true so basically it isn't bounded to object anymore and new state and value properties are created on window object !
I am trying to implement the original promise function to see if i can do so and then i will go through the details of how it is actually implemented .