标签:状态 ack 就会 instance executor eject nal reason index
优点:解决并发问题(同步多个异步方法的执行结果)。解决链式调用的问题(先获取a,在获取b) 解决多个回调嵌套的问题。缺点:promise无法终止,只能说是抛弃本次promise的结果。
npm i promises-aplus-tests -g
在promise.js
增加以下代码
Promise.deferred = function(){
let dfd = {};
dfd.promise = new Promise((resolve,reject)=>{
dfd.resolve = resolve;
dfd.reject = reject;
});
return dfd;
}
promises-aplus-tests promise.js
捕获失败的peomise。
无论如何都执行,finally会传递前一个promise的结果但不会接管该结果,如果finally返回的是promise会等待这个promise执行完成。
能捕获同步或者异步异常。需要自己实现。
返回成功的promise。
返回失败的pomise。
返回promise。全部完成才算完成,如果有一个失败,就失败。Promise.all是按照顺序执行的。
有一个成功就成功,有一个失败就失败。
终止一个promise。
const PENDING = 'PENDING';
const FULFILLED = 'FULFILLED';
const REJECTED = 'REJECTED';
const resolvePromise = (promise2, x, resolve, reject) => {
if (promise2 === x) {
return reject(new TypeError());
}
if ((x !== null && typeof x === 'object') || typeof x === 'function') {
let isCalled = false;
try {
let then = x.then;
if (typeof then === 'function') {
then.call(x, y => {
if (isCalled) return;
isCalled = true;
resolvePromise(promise2, y, resolve, reject)
}, r => {
if (isCalled) return;
isCalled = true;
reject(r);
})
} else {
resolve(x);
}
} catch (e) {
if (isCalled) return;
isCalled = true;
reject(e)
}
} else {
resolve(x);
}
}
class Promise {
constructor(executor) {
this.status = PENDING;
this.value = null;
this.reason = null;
this.onFulfilledCallbacks = [];
this.onRejectedCallbacks = [];
let resolve = value => {
if (this.status === PENDING) {
if (value instanceof Promise) {
return value.then(resolve, reject);
}
this.status = FULFILLED;
this.value = value;
this.onFulfilledCallbacks.forEach(fn => fn())
}
}
let reject = reason => {
if (this.status === PENDING) {
this.status = REJECTED;
this.reason = reason;
this.onRejectedCallbacks.forEach(fn => fn())
}
}
try {
executor(resolve, reject);
} catch (e) {
reject(e)
}
}
then(onFulfilled, onRejected) {
onFulfilled = typeof onFulfilled == 'function' ? onFulfilled : value => value;
onRejected = typeof onRejected == 'function' ? onRejected : err => { throw err };
let promise2 = new Promise((resolve, reject) => {
if (this.status === FULFILLED) {
setTimeout(() => {
try {
let x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e)
}
})
}
if (this.status === REJECTED) {
setTimeout(() => {
try {
let x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e)
}
})
}
if (this.status === PENDING) {
this.onFulfilledCallbacks.push(() => {
setTimeout(() => {
try {
let x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e)
}
})
});
this.onRejectedCallbacks.push(() => {
setTimeout(() => {
try {
let x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e)
}
})
});
}
});
return promise2;
}
catch(onRejected) {
return this.then(null, onRejected);
}
static all(promises) {
return new Promise((resolve, reject) => {
let values = [], index = 0, len = promises.length;
promises.forEach((promise, i) => {
Promise.resolve(promise).then((v) => {
values[i] = v;
++index === len ? resolve(values) : null;
}, reject)
})
})
}
static resolve(value) {
return new Promise((resolve) => {
resolve(value);
})
}
static reject(value) {
return new Promise((resolve, reject) => {
reject(value);
})
}
finally(fn) {
return this.then(v => Promise.resolve(fn()).then(() => v), e => Promise.resolve(fn()).then(() => { throw e }));
}
static stop(promise) {
let fail;
let promise2 = new Promise((resolve, reject) => {
fail = reject;
})
let race = Promise.race([promise2, promise]);
race.reject = fail;
return race;
}
static try(fn) {
return new Promise((resolve, reject) => {
resolve(fn())
})
}
static race(promises) {
return new Promise((resolve, reject) => {
promises.forEach(promise => {
Promise.resolve(promise).then(resolve, reject);
})
})
}
}
Promise.deferred = function () {
const dfd = {};
dfd.promise = new Promise((resolve, reject) => {
dfd.resolve = resolve;
dfd.reject = reject;
})
return dfd;
}
module.exports = Promise;
标签:状态 ack 就会 instance executor eject nal reason index
原文地址:https://www.cnblogs.com/wangshouren/p/12291743.html