function Promise(task) {
let that = this;
that.status = 'pending';
that.value = undefined;
that.ResolvCallback = [];
that.RejectCallback = [];
function resolve(value) {
if (that.status == 'pending') {
that.status = 'fulfilled';
that.value = value;
that.ResolvCallback.forEach(callback => callback(that.value));
}
}
function reject(reason) {
if (that.status == 'pending') {
that.status = 'rejected';
that.value = reason;
that.RejectCallback.forEach(callback => callback(that.value))
}
}
try {
task(resolve, reject)
} catch (error) {
reject(error)
}
}
Promise.prototype.then = function (onFulfilled, onRejected) {
let that = this;
if (this.status == 'fulfilled') {
onFulfilled(that.value)
}
if (this.status == 'rejected') {
onRejected(that.value)
}
if (this.status == 'pending') {
that.RejectCallback.push(onFulfilled);
that.RejectCallback.push(onRejected);
}
return that;
}
var promise = new Promise((resolve, reject) => {
resolve(123)
})
promise.then((res) => {
console.log(res + 100);
return res + 100;
}).then((res) => {
console.log(res)
})
执行结果:
223
123
改版一
function Promise(task) {
let that = this;
that.status = 'pending';
that.value = undefined;
that.ResolvCallback = [];
that.RejectCallback = [];
function resolve(value) {
if (that.status == 'pending') {
that.status = 'fulfilled';
that.value = value;
that.ResolvCallback.forEach(callback => callback(that.value));
}
}
function reject(reason) {
if (that.status == 'pending') {
that.status = 'rejected';
that.value = reason;
that.RejectCallback.forEach(callback => callback(that.value))
}
}
try {
task(resolve, reject)
} catch (error) {
reject(error)
}
}
Promise.prototype.then = function (onFulfilled, onRejected) {
let that = this;
var promise2;
if (this.status == 'fulfilled') {
promise2 = new Promise((resolve, reject) => {
let x = onFulfilled(that.value);
resolve(x)
})
}
if (this.status == 'rejected') {
promise2 = new Promise((resolve, reject) => {
let x = onRejected(that.value);
reject(x)
})
}
if (this.status == 'pending') {
that.ResolvCallback.push(onFulfilled);
that.RejectCallback.push(onRejected);
}
return promise2;
}
var promise = new Promise((resolve, reject) => {
resolve(123);
// reject(123);
})
promise.then((res) => {
console.log(res + 100);
return res + 100;
}, err => 'err')
.then((res) => {
console.log(res)
},err=>console.log('err'))
结果值:
223
223
Promise.catch
//catch原理就是只传失败的回调
Promise.prototype.catch = function (onRejected) {
this.then(null, onRejected);
}
Promise.all
参数:接受一个数组,数组内都是Promise实例
返回值:返回一个Promise实例,这个Promise实例的状态转移取决于参数的Promise实例的状态变化。当参数中所有的实例都处于resolve状态时,返回的Promise实例会变为resolve状态。如果参数中任意一个实例处于reject状态,返回的Promise实例变为reject状态
Promise.all = function (promises) {
return new Promise((resolve, reject) => {
let done = gen(promises.length, resolve);
for (let i = 0; i < promise.length; i++) {
promises[i].then(res => {
done(i, res)
}, reject)
}
})
}
function gen(i, val) {
let count = 0, result = []
return function (i, val) {
result[i] = val
if (++count === i) {
resolve(result)
}
}
}
var promise = new Promise((resolve, reject) => {
resolve(123);
// reject(123);
})
var promise1 = new Promise((resolve, reject) => {
resolve(456);
// reject(123);
})
promise.then((res) => {
console.log(res + 100);
return res + 100;
}, err => 'err')
promise1.then(res=>{console.log(res)},err=>console.log('err'))
var all = Promise.all([].concat(promise,promise1))
all.then((res)=>{
console.log(res)
},err=>console.log('err'))
Promise.resolve
/**
* 返回一个立刻成功的promise,
* 别人提供给你一个方法,需要你传入一个promise,
* 但你只有一个普通的值,你就可以通过这个方法把这个普通的值(string number object)转成一个promise对象
*/
Promise.resolve = function (value) {
return new Promise(resolve => resolve(value))
}
**Promise. reject**
//返回一个立刻失败的promise
Promise.reject = function (value) {
return new Promise((resolve,reject) => reject(value))
}
Promise.race
参数:接受一个数组,数组内都是Promise实例
返回值:返回一个Promise实例,这个Promise实例的状态转移取决于参数的Promise实例的状态变化。哪个promise返回的快就返回哪个,无论状态是resolve还是reject。
Promise.race = function (promises) {
return new Promise((resolve, reject => {
for (let i = 0; i < promises.length; i++) {
promises[i].then(resolve,reject)
}
}))
}