Promise有几种状态? pending fulfilled rejected
Promise有哪些方法? resolve reject
Promise有哪些属性? value reason
Promise有一个then方法
class Promise {constructor (fn){this.state = "pending" //promise的状态this.value = undefined //resolve函数传递的参数this.reason = undefined //reject函数传递的参数//resolve函数将promise的状态改为fulfilledlet resolve = value =>{if(this.state === 'pending'){this.state = 'fulfilled'this.value = value}}//resolve函数将promise的状态改为fulfilledlet reject = value =>{if(this.state === 'pending'){this.state = 'reject'this.reason = value}}//让构造函数入参的函数立马执行try {fn (resolve,reject)} catch (e) {reject (e)}}//Promise的then方法入参两个函数,根据当前promise对象的state状态信息,决定是成功的回调函数执行还是失败的回调函数执行then(onFulfilled,onRejected) {switch (this.state) {case 'fulfilled':onFulfilled(this.value)breakcase 'rejected':onRejected(this.reason)breakdefault:}}}//1.Promise是一个构造函数,需要入参一个函数,该函数的参数需要入参两个函数,而且该函数是立马执行的//2.Promise有三个状态:pending进行中 fulfilled成功 rejected失败//3.Promise有两个值:value是promise成功之后返回的值,reason是promise失败之后返回的原因//4.Promise有两个函数:resolve用于处理成功的函数,reject用于处理失败的函数//5.Promise的then方法可以入参两个函数,第一个是promise成功的回调,第二个是promise失败的回调var p = new Promise(function(resolve,reject){resolve(666);})p.then(function(res){console.log(res)})

lass Promise {constructor(fn) {this.state = "pending";this.value = null;this.reason = null;this.then = function (onFulfilled, onRejected) {//执行then方法的时候,需要查看Promise的状态,如果Promise执行成功,需要执行成功的的回调;如果Promise失败,需要执行Promise失败的回调switch (this.state) {case 'fulfilled':onFulfilled(this.value);break;case 'reject':onRejected(this.reason);break;}};let resolve = value => {if (this.state === 'pending') {this.state = "fulfilled";this.value = value;}}let reject = reason => {if (this.state === 'pending') {this.state = "reject";this.reason = reason;}}//执行fn函数try {fn(resolve, reject)} catch (e) {reject(e)}}}var p = new Promise(function (resolve, reject) {console.log("22222")resolve("小明");})//Promise.then方法接收两个函数做为参数//第一个函数参数是Promise成功的回调//第二个函数参数是Promise失败的回调p.then((msg) => {console.log(msg,"AAAAAAAAAA")}, (err) => {console.log(err,"BBBBBBBBBBBBBBB");})

