先看一个例子
var Promise = require("promise-tiny");
new Promise(function(resolve, reject) { // 注重resolve和reject这两个参数,现实就是then和catch的参数
var r = Math.random();
if(r >= 0.5) resolve('success');
else reject('fail');
})
.then(function(value) { // >=0.5时,挪用这个函数
console.log(value);
})
.catch(function(value) { // <0.5时,挪用这个函数
console.log(value);
});
promise-tiny的完成代码
class Promise { // 这段代码重要用于展现完成道理,对明白异步编程技能很有协助
constructor(factory) {
this.flag = 'Pending'; // flag值域 'Pending','Resolved','Rejected'
this.args = [];
this.func = {};
function next(flag, value) { // next这个函数是英华,factory没有参数运转起来,端赖它了
this.flag = flag;
this.args = [].concat(value);
this.func[flag] && this.func[flag].apply(undefined, this.args);
}
factory(next.bind(this, 'Resolved'), next.bind(this, 'Rejected'));
}
then(func) {
if(this.flag==='Resolved') func.apply(undefined, this.args);
else this.func['Resolved'] = func;
return this;
}
catch(func) {
if(this.flag==='Rejected') func.apply(undefined, this.args);
else this.func['Rejected'] = func;
return this;
}
}
明白了道理,就以为应该能完成的更好。照样先看一个例子
var Steps = require("promise-tiny/Steps");
class Count {
constructor() {
this._step = 0;
}
get step() {
return this._step;
}
set step(n) {
this._step = n;
}
}
new Steps(new Count)
.on('Begin', function(next) {
this.step++;
next('check', 'Begin');
})
.on('check', function(next, ...args) {
this.step++;
next('create', [].concat(args, 'check'));
})
.on('create', function(next, ...args) {
this.step++;
next('error', [].concat(args, 'create'));
})
.on('logout', function(next, ...args) {
this.step++;
next('End', [].concat(args, 'logout'));
})
.on('error', function(next, ...args) {
this.step++;
next('End', [].concat(args, 'error'));
})
.on('End', function(next, ...args) {
this.step++;
console.log('Steps: '+this.step, 'trace: '+[].concat(args, 'End').join('->'));
next('new Steps', { id: '!Count', count: 0 });
})
.on('Begin', function(next, ...args) {
this.count++;
next('info', [].concat(args, 'Begin'));
})
.on('info', function(next, ...args) {
this.count++;
next('logout', [].concat(args, 'info'));
})
.on('logout', function(next, ...args) {
this.count++;
next('End', [].concat(args, 'logout'));
})
.on('error', function(next, ...args) {
this.count++;
next('End', [].concat(args, 'error'));
})
.on('End', function(next, ...args) {
this.count++;
console.log('Count: '+this.count, 'trace: '+[].concat(args, 'End').join('->'), this.id);
});
效果
Steps: 5 trace: Begin->check->create->error->End
Count: 4 trace: new Steps->Begin->info->logout->End !Count
Promise代码体味
带有一个函数参数的函数 f1(f2) ,可以先造一个f2’,如许就可以把它实行了 f1(f2’)。
这个f2’的功用要如许完成:当实行到f2’时,f2’要搜检实在的f2是不是已预备好了?假如预备好了,就挪用实在的f2;不然,要把挪用f2的参数都记下来,等f2预备好时挪用。
如许就不须要请求挪用f1时,f2必需预备好了。但分外要供应一个提交f2的要领。
以上就是Promise展现的异步编程技能。在Promise中,factory是f1;resolve和reject是f2’;then和catch是提交f2的要领。
Promise展现的编程技能为何能改良异步编程要领?
这重要是因为程序员编写程序时,老是根据本身的思索习气和代码构造习气编写程序,倾向于同步实行历程。代码的提交序次与机械实行序次有着很大差别!Promise展现的技能使程序员可以不必斟酌机械的实行序次,给点代码就先实行着,遇到没给的代码就记录下来,等后续代码提交后接着实行。如许,程序员只需保证终究把一切代码都提交就可以了。
应该有更好的完成
既然有如许好的思绪,再回头看看Promise的完成,个中缺点显而易见。Steps是一次尝试,斟酌的题目要比Promise多一些。