js中Promise的状态探究
1、promise可以处于等待被赋值的等待态(pending),可以给出值并转换为解决态(resolved)。
2、一旦promise被一个值resolve丢失,它将始终保持这个值不再被resolve丢失。
实例
代码语言:javascript复制function Promise(fn) {
var state = 'pending';
var value;
var deferred;
function resolve(newValue) {
value = newValue;
state = 'resolved';
if(deferred) {
handle(deferred);
}
}
function handle(onResolved) {
if(state === 'pending') {
deferred = onResolved;
return;
}
onResolved(value);
}
this.then = function(onResolved) {
handle(onResolved);
};
fn(resolve);
}
以上就是js中Promise的状态探究,希望对大家有所帮助。更多js学习指路:js教程
收藏 | 0点赞 | 0打赏