有了之前处理fulfilled状况的经验,支掉足误处理变得很轻易,只须要在注册回调、处理状况变革上都要参加新的逻辑:
- function Promise(fn) {
- var state = 'pending',
- value = null,
- callbacks = [];
- this.then = function (onFulfilled, onRejected) {
- return new Promise(function (resolve, reject) {
- handle({
- onFulfilled: onFulfilled || null,
- onRejected: onRejected || null,
- resolve: resolve,
- reject: reject
- });
- });
- };
- function handle(callback) {
- if (state === 'pending') {
- callbacks.push(callback);
- return;
- }
- var cb = state === 'fulfilled' ? callback.onFulfilled : callback.onRejected,
- ret;
推荐阅读
16. 美国法律论坛71万账户泄漏,涉国度安然局、FBI等本年4月,达拉斯紧急警备孀体系被黑客入侵。导致该城市的156个紧急戒备器被激活,警笛声持续一个小时,激发市平易近惊恐。 在以前的201>>>详细阅读
本文标题:30分钟,让你彻底明白Promise原理
地址:http://www.17bianji.com/lsqh/35631.html
1/2 1