首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OOP Javascript使用Promise和"this“的问题

OOP Javascript使用Promise和"this“的问题
EN

Stack Overflow用户
提问于 2018-01-03 21:39:26
回答 2查看 334关注 0票数 0

在设计OOP ES 5-6时,我将原型与ES 6函数相结合。所有的东西都在ElectronJS中运行,我不想要一个完全支持ES 7的新版本,所以例如,从"./cesta“导入{trida}的定义必须解决('./ path')。我的问题,但这是我的承诺。

如果我想对对象进行编程,我希望每个对象都做它所拥有的事情,并将结果传递给程序的主分支,该分支包含整个过程的列-对象列表-内容。如果是fun enter setTimeout (),则需要在此处使用Promise,它将等待函数执行并继续调用另一个对象。

代码语言:javascript
复制
let importing = function(){
	this.name = "boot";
}

importing.prototype.start = function(resolve){
    this.resolve = resolve;
    setTimeout(this.test.bind(this),1000);
	console.log('start -------->');
}

importing.prototype.test = function(){
    this.resolve('Test');
	console.log('Test -------->');
}


importing.prototype.end = function(resolve){ 
	console.log('end -------->');
    this.resolve = resolve;
    this.resolve('end');
}

let geko;
let scan = new importing();
Promise.resolve(geko)
	.then((geko) => new Promise(scan.start.bind(scan)))
	.then((geko) => new Promise(scan.end.bind(scan)))
	.catch(geko => {
	    console.log('Error message: ',geko)
	})

这就是问题所在,我不想让这些功能嵌套在原型函数中,我想调用每个对象,很好地按顺序,清晰地调用。像任何一本书一样,它有自己的章节内容,然后章节本身和我想要快速进入我编程的内容,而不是看一周后鼠标被占用了多少。但是对于这个操作,除了Promise之外,我还必须使用bind ()函数,因为:

代码语言:javascript
复制
importing.prototype.start = function(){
// here this acts as a window object not as importing
// why I have this object called scan.start.bind (scan)
// and here again in setTimeout i have to bind (this) to get it in this.test
// could access this importing - scan object
  setTimeout(this.test.bind(this),300);
}

你会找到更好的方法??

EN

回答 2

Stack Overflow用户

发布于 2018-01-04 03:39:16

不应将方法作为参数传递给new Promise构造函数。如果方法是异步的,那么它本身应该返回promise;如果它不是异步的,那么您就不应该使用任何promise。

代码语言:javascript
复制
function importing(){
    this.name = "boot";
}

importing.prototype.start = function() {
    console.log('start -------->');
    return new Promise(resolve => {
        setTimeout(resolve, 1000); // put the native async call in here, and nothing else!
    }).then(() =>
        this.test()
    );
};

importing.prototype.test = function() {
    console.log('Test -------->');
    return 'Test';
};

importing.prototype.end = function() { 
    console.log('end -------->');
    return 'end';
}

const scan = new importing();
scan.start().then(() => scan.end()).catch(geko => {
    console.log('Error message: ',geko)
});
票数 0
EN

Stack Overflow用户

发布于 2018-01-04 16:47:48

如果整个项目是按如下方式创建的:

代码语言:javascript
复制
return new Promise(resolve => {
        setTimeout(resolve, 1000);
    }).then(() =>
        this.test()
    );

我不会对经典的JS过程做太多的工作,也就是在函数中嵌套函数。这就是我想要避免的。我想要的是内容,一个大纲,当我花一年的时间观察它,我会解决这些bug,我会知道从哪里开始,哪里会出错。

代码语言:javascript
复制
let importing = function(){
	this.name = "boot";
}

importing.prototype.start = function(resolve){
	console.log('Start');
    this.resolve = resolve;
    setTimeout(this.test.bind(this),1000);
}

importing.prototype.test = function(){
	console.log('Test');
    this.resolve('Test');
}

importing.prototype.end = function(resolve){
	console.log('End');
    resolve('end');
}

let scan = new importing();
let promise = function(arg){
    return new Promise((resolve, reject)=>{ // The same: new Promise(scan[arg].bind(scan))
		return scan[arg].bind(scan)(resolve)
	});
}

// so I would imagine chaining. Few braces, simply
// In each called object, if possible, the minimum promissory note

Promise.resolve()
	.then(geko => promise('start'))
	.then(geko => promise('end'))
	.catch(geko => {
	    console.log('Error message: ',geko)
	})

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48078570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档