自制ES6 Promise
来源:互联网 发布:深圳软件学校 编辑:程序博客网 时间:2024/05/01 15:21
Checked all Promise sample, none of them is in ES6 syntax, this is weird, which age are we now. Simplest verison
class MyPromise { constructor(fn) { this.doneCallback = null; fn(this.resolve.bind(this)); // resolve will be called in another function context, must bind here } resolve(result) { this.doneCallback(result); } then(done) { this.doneCallback = done; return this; }}function test() { return new MyPromise(function (successResolve) { setTimeout(function () { console.log(100); successResolve(1); }, 1000); });}test().then((d) => { console.log('done' + d);});
Added Promise chain.
class MyPromise { constructor(fn) { this.doneList = []; fn(this.resolve.bind(this)); // resolve will be called in another function context, must bind here } resolve(result) { var self = this; setTimeout(function () { self.doneList.forEach(function (fulfill) { fulfill(result); }); }, 0); } then(done) { this.doneList.push(done); return this; }}function test() { return new MyPromise(function (successResolve) { setTimeout(function () { successResolve(1); }, 1000); });}test().then((d) => { console.log('step1: ' + d);}).then(_ => { console.log('step2:');}).then(m => { console.log('step3: ' + m);});
阅读全文
0 0
- 自制ES6 Promise
- ES6--Promise
- ES6 Promise
- ES6-Promise
- ES6 Promise
- ES6 Promise
- es6 promise
- Promise ES6
- ES6 Promise
- ES6--promise
- ES6: Promise
- ES6 Promise
- es6 Promise
- 【ES6】Promise
- ES6-Promise
- ES6 Promise
- es6 promise
- es6 promise对象
- USB协议介绍[6]-描述符和设备类
- C++经典习题
- USB协议介绍[7]-协议层(完)
- Highway network
- 五大常用算法:分治、动态规划、贪心、回溯和分支界定
- 自制ES6 Promise
- oracle 比较函数
- python 笔记 更多的变量和字符串(string) ——12.22
- CSS实现网页背景图片自适应全屏
- C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名
- SqlServer2008R2 分区管理
- cv::pyrMeanShiftFiltering 和 cuda::meanShiftSegmentation源码阅读笔记
- 第三章 ALDS1_2_D:Shell Sort 希尔排序法
- JavaScript 继承---组合继承