在设计OOP ES 5-6时,我将原型与ES 6函数相结合。所有的东西都在ElectronJS中运行,我不想要一个完全支持ES 7的新版本,所以例如,从"./cesta“导入{trida}的定义必须解决('./ path')。我的问题,但这是我的承诺。
如果我想对对象进行编程,我希望每个对象都做它所拥有的事情,并将结果传递给程序的主分支,该分支包含整个过程的列-对象列表-内容。如果是fun enter setTimeout (),则需要在此处使用Promise,它将等待函数执行并继续调用另一个对象。
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 ()函数,因为:
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);
}
你会找到更好的方法??
发布于 2018-01-04 03:39:16
不应将方法作为参数传递给new Promise构造函数。如果方法是异步的,那么它本身应该返回promise;如果它不是异步的,那么您就不应该使用任何promise。
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)
});发布于 2018-01-04 16:47:48
如果整个项目是按如下方式创建的:
return new Promise(resolve => {
setTimeout(resolve, 1000);
}).then(() =>
this.test()
);
我不会对经典的JS过程做太多的工作,也就是在函数中嵌套函数。这就是我想要避免的。我想要的是内容,一个大纲,当我花一年的时间观察它,我会解决这些bug,我会知道从哪里开始,哪里会出错。
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)
})
https://stackoverflow.com/questions/48078570
复制相似问题