js中Promise的状态探究
小妮浅浅
2021-10-29 11:21:426968浏览 · 0收藏 · 0评论
1、promise可以处于等待被赋值的等待态(pending),可以给出值并转换为解决态(resolved)。
2、一旦promise被一个值resolve丢失,它将始终保持这个值不再被resolve丢失。
实例
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教程
关注公众号,随时随地在线学习