Saturday 29 September 2018

newbie question: issue with using "this" within promise

Hey guys,Sorry for the newbie question. This question will definitely expose my lack of understanding of javascript classes.So I'm trying to write a class with methods that return a promise. It seems that within the promise function, the "this" operator no longer refers to the class but is undefined.Here is a useless class that demonstrates what I am talking about:class foo { constructor(data) { this.bar = data; } set(data) { return new Promise(function(fulfill, reject) { this.bar = data; fulfill(); }); } }So obviously this is a fake example but "this" in foo.set no longer refers to foo when in the promiseThanks!!

Submitted September 30, 2018 at 03:12AM by thenewstampede

No comments:

Post a Comment