1、Promise.js
console.log('my')
const RESOLVED = 'RESOLVED';
const REJECTED = 'REJECTED';
const PENDING = 'PENDING';
const resolvePromise=(promsie2,x,resolve,reject)=>{
console.log(promsie2,x,resolve,reject);
}
class Promise {
constructor(executor) {
this.status = PENDING;
this.value = undefined;
this.reason = undefined;
this.onResolvedCallbacks = []; //存放成功回调
this.onRejectedCallbacks = []; // 存放失败的回调
let resolve = (value) => { //调用此方法就是成功
if (this.status === PENDING) {
this.value = value;
this.status = RESOLVED;
this.onRejectedCallbacks.forEach(fn => fn())
}
}
let reject = (reason) => {
if (this.status == PENDING) {
this.reason = reason;
this.status = REJECTED;
this.onRejectedCallbacks.forEach(fn => fn())
}
}
try {
executor(resolve, reject); //立即执行
} catch (e) {
reject(e);
}
}
then(onFulfilled, onRejected) {
let promsie2 = new Promise((resolve, reject) => {
if (this.status === RESOLVED) {
onFulfilled(this.value);
setTimeout(() => {
try {
let x = onFulfilled(this.value);
resolvePromise(promsie2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
}
if (this.status === REJECTED) {
onRejected(this.reason);
setTimeout(() => {
try {
let x = onFulfilled(this.value);
resolvePromise(promsie2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
}
if (this.status === PENDING) {
console.log("等待");
this.onResolvedCallbacks.push(() => {
onFulfilled(this.value);
setTimeout(() => {
try {
let x = onFulfilled(this.value);
resolvePromise(promsie2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
});
this.onRejectedCallbacks.push(() => {
onRejected(this.value);
setTimeout(() => {
try {
let x = onFulfilled(this.value);
resolvePromise(promsie2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
})
}
});
return promsie2;
}
}
module.exports = Promise;
2、Promise2.js
let fs=require('fs');
function read(filename){
return new Promise((resolve,reject)=>{
fs.readFile(filename,'utf8',(err,data)=>{
if(err) return reject(err);
resolve(data);
})
})
}
read("./name.txt").then((data)=>{
return 100;
},(err)=>{
return 200;
}).then((data)=>{
console.log('-----',data);
},(err)=>{
console.log('------',err+'错误');
})
3、Promise1.js
// promsie aplus 规范
// https://promisesaplus.com/ 都通过这个规范来实现 promsie es6 中已经内置了 IE不支持promise 需要polyfill es6-promise
// promsie 为什么会产生 解决异步问题
// 1.多个异步请求并发 (希望同步最终的结果) promsie.all
// 2.链式异步请求的问题 上一个人的输出是下一个人的输入 Promise的链式调用可以解决这个问题
// 3.缺陷:还是基于回调的 成功态resolve 失败态reject 等待态pending (不成功 不失败)
// 4.promise 默认执行器时立即执行
// 5.promsie 每一个实例都具备一个then方法 两个参数 一个是成功的回调 一个是失败的回调
// 6. 如果执行函数时发生了异常 也会执行失败逻辑
// 7.如果promise 一旦成功 就不能失败 反过来也是一样的
// 8.只有等待态的时候才能去更改状态
// 9. promise 调用this方法时 可能当前的promise并没有成功 pending
// 10. 发布订阅模式 如果当前状态是pending时 我们需要将成功的回调和失败的回调 存放起来 稍后调用resolve() 和reject的时候 重新执行
// 异步相关的问题 都会用到发布订阅模式
// 如果返回值是普通值的话,可能还要promsie的情况 出错的情况 (一定会走到下一次的失败) 不是错误,不是Promise 就是普通值
// promsie就是一个类
let Promise=require('./promise');
let promsie=new Promise((resolve,reject)=>{
//resolve(1);
console.log(1);
resolve("成功")
})
console.log(2);
promsie.then((data)=>{
console.log("success",data);
},(data)=>{
console.log("success",data);
});