自己写的一个简单的 promise 库
发布于 8 年前 作者 jackielin 4262 次浏览 来自 分享

地址:Promise

介绍

promise

promise A+ 规范的简单实现

1. 使用

点击 这里 下载,可以通过两种方式引入:

1. 创建 script 标签 <script src="<Your path>/promise.js"></script>
2. 通过 requirejs 等支持 AMD 规范的库引入

2. 语法

// 新建
promise = new Promise(function(resolve, reject) {
  return window.setTimeout(function() {
    return resolve(111);
  }, 1000);
});

// then
promise.then(function(res) {
  return 333;
}).then(function(res) {
	// 执行方法体
});

// then 返回 promise
promise.then(function(res) {
  return new Promise(function(resolve, reject) {
    window.setTimeout(function() {
      resolve(222);
    }, 500);
  }).done(function(res) {
    // done 方法必须写,否则 promise 不会返回
    return res;
  });
}).then(function(res) {
  return res;
});

// then fail
new Promise(function(resolve, reject) {
  reject('error');
}).fail(function(res) {
  console.log(res);
});

3. 协议

promise 采用 MIT 协议

欢迎各位的意见以及拍砖

4 回复

promise的本质是将函数注册到队列,将实际的函数延后执行吗?

post github url plz

@yakczh 我是这么理解的,promise 其实是回调的一个语法糖

@qiusli 什么意思

回到顶部