三个状态
promise对象
then 方法链式实现
promise =
{
then: function(callback) {
callback();
}
}
手写实现
:::info
使用 hack 先实现 resolve 时,callback 已经经过 then 中 callback = cb
:::
function Promise(fn) {
var callback = null;
this.then = function (cb) {
callback = cb;
};
function resolve(value) {
setTimeout(function () {
// hack
callback(value);
}, 0);
}
fn(resolve);
}
var p = new Promise((resolve) => {
resolve(666);
}).then((data) => {
console.log("Got a value: " + data);
});
:::info 使用 promise 状态,不使用 hack 方法 :::
function Promise(fn) {
var state = "pending";
var value;
this.then = function (onResolved) {
handle(onResolved);
};
function resolve(newValue) {
value = newValue; // value -> 666
state = "resolved"; // pending -> resolved
}
function handle(onResolved) {
onResolved(value);
}
fn(resolve); // 在 new 的时候执行 resolve
}
var p = new Promise((resolve) => {
resolve(666);
}).then((data) => {
console.log("Got a value: " + data);
});
:::info 实现链式操作,每个 then 都会返回 promise 对象 :::
function Promise(fn) {
var state = "pending";
var value;
function resolve(newValue) {
value = newValue;
state = "resolved";
}
function handle(handler) {
if (!handler.onResolved) { // 处理只调用 then 没传 onResolved 函数,让下一个 then 可以接
handler.resolve(value);
return;
}
var returnValue = handler.onResolved(value);
handler.resolve(returnValue);
}
this.then = function (onResolved) {
return new Promise(function (resolve) { // 每个 then 都会返回 promise 对象
handle({
onResolved: onResolved,
resolve: resolve,
});
});
};
fn(resolve);
}
var p = new Promise((resolve) => {
resolve(666);
})
.then()
.then((data) => {
console.log("Got a value: " + data);
return 88;
})
.then((secondResult) => {
console.log(secondResult);
});
:::info 处理 then 返回 promise 而不是 原始值 的情况,用 then 即可以取回值 :::
function Promise(fn) {
var state = "pending";
var value;
function resolve(newValue) {
value = newValue;
state = "resolved";
}
function handle(handler) {
if (!handler.onResolved) {
handler.resolve(value);
return;
}
var returnValue = handler.onResolved(value);
handler.resolve(returnValue);
}
this.then = function (onResolved) {
return new Promise(function (resolve) {
handle({
onResolved: onResolved,
resolve: resolve,
});
});
};
fn(resolve);
}
function doSomething() {
return new Promise(function (resolve) {
resolve(666);
});
}
function doSomethingElse(value) {
return new Promise(function (resolve) {
resolve("did something else with " + value);
});
}
var p = doSomething()
.then()
.then((data) => {
console.log("Got a value: " + data);
return doSomethingElse(data);
})
.then((anotherPromise) => {
anotherPromise.then(function(value){ // 如果是返回的是 promise 还要用 then
console.log("Got anotherPromise: " + value);
})
});
function Promise(fn) {
var state = "pending";
var value;
function resolve(newValue) {
if (newValue && typeof newValue.then === "function") { // 递归处理 resolve return 是 promise
newValue.then(resolve);
return;
}
value = newValue;
state = "resolved";
}
function handle(handler) {
if (!handler.onResolved) {
handler.resolve(value);
return;
}
var returnValue = handler.onResolved(value);
handler.resolve(returnValue);
}
this.then = function (onResolved) {
return new Promise(function (resolve) {
handle({
onResolved: onResolved,
resolve: resolve,
});
});
};
fn(resolve);
}
function doSomething() {
return new Promise(function (resolve) {
resolve(666);
});
}
function doSomethingElse(value) {
return new Promise(function (resolve) {
resolve("did something else with " + value);
});
}
var p = doSomething()
.then()
.then((data) => {
console.log("Got a value: " + data);
return doSomethingElse(data);
})
.then((value) => {
console.log("Got anotherPromise: " + value);
});
:::info rejected 状态实现 :::
function Promise(fn) {
var state = "pending";
var value;
function resolve(newValue) {
if (newValue && typeof newValue.then === "function") {
newValue.then(resolve);
return;
}
value = newValue;
state = "resolved";
}
function reject(reason) {
value = reason;
state = "rejected";
}
function handle(handler) {
var handlerCallback;
if (state === "resolved") {
handlerCallback = handler.onResolved;
} else {
handlerCallback = handler.onRejected;
}
if (!handlerCallback) {
if (state === "resolved") {
handler.resolve(value);
} else {
handler.reject(value);
}
return;
}
var returnValue = handlerCallback(value);
handler.resolve(returnValue);
}
this.then = function (onResolved, onRejected) {
return new Promise(function (resolve, reject) {
handle({
onResolved: onResolved,
onRejected: onRejected,
resolve: resolve,
reject: reject,
});
});
};
fn(resolve, reject);
}
function somehowGetThevalue() {
return Math.random() > 0.5 ? { value: ">0.5" } : { error: "<=0.5" };
}
function doSomething() {
return new Promise(function (resolve, reject) {
var result = somehowGetThevalue();
if (result.error) {
reject(result.error);
} else {
resolve(result.value);
}
});
}
var p = doSomething().then(
(value) => {
console.log("Success!", value);
},
(error) => {
console.log("Fail!", error);
}
);
:::info resolve 时捕捉异常也用 reject 处理 :::
function Promise(fn) {
var state = "pending";
var value;
function resolve(newValue) {
try {
if (newValue && typeof newValue.then === "function") {
newValue.then(resolve);
return;
}
value = newValue;
state = "resolved";
} catch (e) {
reject(e);
}
}
function reject(reason) {
value = reason;
state = "rejected";
}
function handle(handler) {
var handlerCallback;
if (state === "resolved") {
handlerCallback = handler.onResolved;
} else {
handlerCallback = handler.onRejected;
}
if (!handlerCallback) {
if (state === "resolved") {
handler.resolve(value);
} else {
handler.reject(value);
}
return;
}
var returnValue;
try {
returnValue = handlerCallback(value);
} catch (e) {
handler.reject(e);
return;
}
handler.resolve(returnValue);
}
this.then = function (onResolved, onRejected) {
return new Promise(function (resolve, reject) {
handle({
onResolved: onResolved,
onRejected: onRejected,
resolve: resolve,
reject: reject,
});
});
};
fn(resolve, reject);
}
function somehowGetThevalue() {
return Math.random() > 0.5 ? { value: ">0.5" } : { error: "<=0.5" };
}
function doSomething() {
return new Promise(function (resolve, reject) {
var result = somehowGetThevalue();
if (result.error) {
reject(result.error);
} else {
resolve(result.value);
}
});
}
var p = doSomething().then(
(value) => {
console.log("Success!", value);
},
(error) => {
console.log("Fail!", error);
}
);
:::info 使 promise 的 then 是异步的过程 :::
function Promise(fn) {
var state = "pending";
var value;
function resolve(newValue) {
try {
if (newValue && typeof newValue.then === "function") {
newValue.then(resolve);
return;
}
value = newValue;
state = "resolved";
} catch (e) {
reject(e);
}
}
function reject(reason) {
value = reason;
state = "rejected";
}
function handle(handler) {
setTimeout(function () {
var handlerCallback;
if (state === "resolved") {
handlerCallback = handler.onResolved;
} else {
handlerCallback = handler.onRejected;
}
if (!handlerCallback) {
if (state === "resolved") {
handler.resolve(value);
} else {
handler.reject(value);
}
return;
}
var returnValue;
try {
returnValue = handlerCallback(value);
} catch (e) {
handler.reject(e);
return;
}
handler.resolve(returnValue);
}, 1);
}
this.then = function (onResolved, onRejected) {
return new Promise(function (resolve, reject) {
handle({
onResolved: onResolved,
onRejected: onRejected,
resolve: resolve,
reject: reject,
});
});
};
fn(resolve, reject);
}
function somehowGetThevalue() {
return Math.random() > 0.5 ? { value: ">0.5" } : { error: "<=0.5" };
}
function doSomething() {
return new Promise(function (resolve, reject) {
var result = somehowGetThevalue();
if (result.error) {
reject(result.error);
} else {
resolve(result.value);
}
});
}
console.log(1);
var p = doSomething().then(
(value) => {
console.log("Success!", value);
},
(error) => {
console.log("Fail!", error);
}
);
console.log(2);