[英]returning ES6 promise from constructor - binding this
I want to do 我想要做
X.prototype.f = function() {
return new Promise(
function(resolve, reject) {
if (this.f1()==0) resolve();
...
however this
(that is the X instance) is not defined inside the promise constructor. 但是, this
(即X实例)未在promise构造函数中定义。 I understand I need to bind this somehow but not sure how to proceed ? 我了解我需要以某种方式绑定此绑定,但不确定如何进行?
As you're using es6, why aren't you using es6? 在使用es6时,为什么不使用es6?
X.prototype.f = function() {
return new Promise((resolve, reject) => {
if (this.f1()==0) resolve();
});
}
You can assign this to another variable inside the function 您可以将其分配给函数内的另一个变量
X.prototype.f = function() {
var self = this;
return new Promise(
function(resolve, reject) {
if (self.f1()==0) resolve();
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.