博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单的 Promise 实现
阅读量:5111 次
发布时间:2019-06-13

本文共 2464 字,大约阅读时间需要 8 分钟。

参考

var PENDING = undefined,    FULLFILLED = 1,    REJECTED = 2;var Promise = function(resolver) {    var promise = this;    this.value;    this.reason;    this.status = PENDING;    this.resolver;    this.rejecter;    var resolve = function(value) {        promise.value = value;        promise.status = FULLFILLED;        if (promise.resolver || promise.rejecter) {            var ret = promise.resolver.call(null, promise.value);            if(ret instanceof Promise) {                ret.then(promise.resolver_relsolve);            } else {                promise.resolver_relsolve(promise.value);            }        }    }    var reject = function(err) {        promise.value = err;        promise.status = REJECTED;        if (promise.resolver || promise.rejecter) {            var ret = promise.rejecter.call(null, promise.value);            if(ret instanceof Promise) {                ret.then(function(){}, promise.rejecter_reject);            } else {                promise.rejecter_reject(promise.value);            }        }    }        resolver(resolve, reject);}// Promise then 方法Promise.prototype.then = function(onFulfilled, onRejected) {    var promise = this;    if (promise.status == FULLFILLED) {        var ret = onFulfilled(promise.value);        if(ret instanceof Promise) {            return ret;        } else {            return new Promise(function(resolve) {                resolve(ret);            });        }    } else if (promise.status == REJECTED) {        var ret = onRejected(promise.value);        if(ret instanceof Promise) {            return ret;        } else {            return new Promise(function(resolve, reject) {                reject(ret);            });        }        }    return new Promise(function(resolve, reject) {        promise.resolver = onFulfilled;        promise.resolver_relsolve = resolve;        promise.rejecter = onRejected;        promise.rejecter_reject = reject;    });}

测试

var p = new Promise(function(resolve, reject) {    console.log('before');    setTimeout(function() {        resolve(10);    }, 2000);});p.then(function() {    console.log('after', arguments);}).then(function() {    return new Promise(function(resolve, reject) {        setTimeout(function() {            resolve(20)        }, 2000);    });}).then(function() {    console.log(arguments);});

输出

// 两秒beforeafter [10]// 两秒[20]
posted on
2016-03-18 09:24 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/wbin91/p/5290790.html

你可能感兴趣的文章
Window7上搭建symfony开发环境(PEAR)
查看>>
Linux内核态、用户态简介与IntelCPU特权级别--Ring0-3
查看>>
第23月第24天 git命令 .git-credentials git rm --cached git stash clear
查看>>
java SE :标准输入/输出
查看>>
一些方便系统诊断的bash函数
查看>>
jquery中ajax返回值无法传递到上层函数
查看>>
css3之transform-origin
查看>>
[转]JavaScript快速检测浏览器对CSS3特性的支持
查看>>
Master选举原理
查看>>
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>
小别离
查看>>
微信小程序-发起 HTTPS 请求
查看>>
WPF动画设置1(转)
查看>>
基于node/mongo的App Docker化测试环境搭建
查看>>
秒杀9种排序算法(JavaScript版)
查看>>
struts.convention.classes.reload配置为true,tomcat启动报错
查看>>
MySQL的并行复制多线程复制MTS(Multi-Threaded Slaves)
查看>>
好玩的-记最近玩的几个经典ipad ios游戏
查看>>
PyQt5--EventSender
查看>>
Sql Server 中由数字转换为指定长度的字符串
查看>>