Baseline Widely available
then()
æ¹æ³åå³ä¸å Promise
ç©ä»¶ã宿¥æ¶å
©å弿¸ï¼ Promise
卿åå失ææ
æ³æçåå¼å½å¼ã
åè¨»ï¼ å¦ææä¸åæå
©å弿¸è¢«çç¥ï¼æçºéå½å¼ï¼non-functionsï¼ï¼å then
å°èæ¼éºå¤± handler(s) ççæ
ï¼ä½ä¸æç¢çé¯èª¤ãè¥ç¼èµ· then
ä¹ Promise
æ¡åäºä¸åçæ
ï¼å¯¦ç¾ï¼fulfillmentï¼
ææçµï¼rejectionï¼ï¼
è then
æ²æèçå®çå½å¼ï¼ä¸åä¸å
·æé¡å¤ handlers çæ° Promise
ç©ä»¶å°è¢«å»ºç«ï¼å®ç´æ¡åå Promise
å
¶æçµçæ
ã
p.then(onFulfilled[, onRejected]);
p.then(function(value) {
// fulfillment
}, function(reason) {
// rejection
});
忏
onFulfilled
ä¸å Function
ï¼ç¶ Promise
被實ç¾ï¼fulfilledï¼æè¢«å¼å«ãæ¤å½å¼æ¥æ¶ä¸å實ç¾å¼ï¼fulfillment value
ï¼ä½çºå¼æ¸ã
onRejected
鏿æ§
ä¸å Function
ï¼ç¶ Promise
被æçµï¼rejectedï¼æè¢«å¼å«ãæ¤å½å¼æ¥æ¶ä¸å失æè¨æ¯ï¼rejection reason
ï¼ä½çºå¼æ¸ã
ä¸åé²å
¥æ±ç½®ï¼pendingï¼çæ
ç Promise
ãï¼åªè¦å çä¸ç©ºï¼handler å½å¼é忥å°ï¼asynchronouslyï¼è¢«å¼å«ãå¨èª¿ç¨ handler å¾ï¼è¥ handler å½å¼ï¼
then
åå³ä¹ promise 以æ¤å¼è¢«å¯¦ç¾ï¼resolvedï¼ãthen
åå³ä¹ promise 以æ¤ä¾å¤è¢«å¦æ±ºï¼rejectedï¼ãthen
åå³ä¹ promise 以æ¤å¼è¢«å¯¦ç¾ãthen
åå³ä¹ promise 以æ¤å¼è¢«å¦æ±ºãthen
åå³ä¹ promise ä¹å¯¦ç¾ï¼æçµé¨å¾ç±èçå½å¼ä¹å¯¦ç¾/妿±ºæ±ºå®ã並ä¸ï¼then
åå³ä¹ promise å°èèçå½å¼åå³ä¹ promise 以ç¸åå¼è¢«è§£æ±ºã以ä¸ä¾åå±ç¤º then
æ¹æ³çé忥æ§è³ªï¼asynchronicityï¼ã
// 使ç¨ä¸å已實ç¾ç promiseï¼'then' åå¡å°ç«å³è¢«è§¸ç¼ï¼ä½æ¯å®ç handlers å°æ¯é忥å°è¢«è§¸ç¼ï¼å¦å console.logs æç¤º
var resolvedProm = Promise.resolve(33);
var thenProm = resolvedProm.then(function (value) {
console.log("æå¨ main stack ä¹å¾è¢«å¼å«ãæ¶å°åå°åå³çå¼çºï¼" + value);
return value;
});
// ç«å³ç´é thenProm
console.log(thenProm);
// æåå¯ä»¥ä½¿ç¨ setTimeout 以延é²ï¼postponeï¼å½å¼å·è¡ç´å°å ççºç©º
setTimeout(function () {
console.log(thenProm);
});
// ç´éçµæï¼ä¾åºçº:
// Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
// "æå¨ main stack ä¹å¾è¢«å¼å«ãæ¶å°åå°åå³çå¼çºï¼33"
// Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: 33}
æè¿°
å çº then
å Promise.prototype.catch()
æ¹æ³é½åå³ promisesï¼å®åå¯ä»¥è¢«ä¸²æ¥ â 稱çºçµåï¼compositionï¼ã
then
æ¹æ³
var p1 = new Promise((resolve, reject) => {
resolve("Success!");
// or
// reject ("Error!");
});
p1.then(
(value) => {
console.log(value); // Success!
},
(reason) => {
console.log(reason); // Error!
},
);
串æ¥
then
æ¹æ³åå³ä¸å Promise
èå¯ä»¥é²è¡æ¹æ³ä¸²æ¥ï¼method chainingï¼ã
妿å³å
¥ then
ç handler å½å¼åå³ä¸å promiseï¼ä¸åçå¹ç Promise
å°è¢«å±ç¾çµ¦æ¹æ³ä¸²æ¥ä¸çä¸ä¸å then ã以ä¸ç¨å¼ç¢¼ç段éé setTimout
å½å¼æ¨¡æ¬é忥ç¨å¼ç¢¼ã
Promise.resolve("foo")
// 1. Receive "foo" concatenate "bar" to it and resolve that to the next then
.then(function (string) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
string += "bar";
resolve(string);
}, 1);
});
})
// 2. receive "foobar", register a callback function to work on that string
// and print it to the console, but not before return the unworked on
// string to the next then
.then(function (string) {
setTimeout(function () {
string += "baz";
console.log(string);
}, 1);
return string;
})
// 3. print helpful messages about how the code in this section will be run
// before string is actually processed by the mocked asynchronous code in the
// prior then block.
.then(function (string) {
console.log(
"Last Then: oops... didn't bother to instantiate and return " +
"a promise in the prior then so the sequence may be a bit " +
"surprising",
);
// Note that `string` will not have the 'baz' bit of it at this point. This
// is because we mocked that to happen asynchronously with a setTimeout function
console.log(string);
});
ç¶ handler å
åå³ä¸åå¼ï¼å¯¦éä¸å®å°åå³ Promise.resolve(<value returned by whichever handler was called>)
.
var p2 = new Promise(function (resolve, reject) {
resolve(1);
});
p2.then(function (value) {
console.log(value); // 1
return value + 1;
}).then(function (value) {
console.log(value + "- This synchronous usage is virtually pointless"); // 2- This synchronous usage is virtually pointless
});
p2.then(function (value) {
console.log(value); // 1
});
è¥å½å¼æåºä¸åé¯èª¤æåå³ä¸åè¢«å¦æ±ºç Promiseï¼then
ä¹å°åå³ä¸åè¢«å¦æ±ºç Promiseã
Promise.resolve()
.then(() => {
// 使 .then() åå³ä¸åè¢«å¦æ±ºç Promise
throw "Oh no!";
})
.then(
() => {
console.log("Not called.");
},
(reason) => {
console.error("onRejected function called: ", reason);
},
);
卿æå
¶ä»æ
å½¢ï¼å¯¦ç¾ä¸ç Promise 被åå³ãå¨ä»¥ä¸ä¾åä¸ï¼ç¬¬ä¸å then()
å°åå³ä¸å實ç¾ä¸å
裹 42 ç promiseï¼å³ä½¿ä¸²æ¥ä¸çåä¸å Promise è¢«å¦æ±ºã
Promise.reject()
.then(
() => 99,
() => 42,
) // onRejected returns 42 which is wrapped in a resolving Promise
.then((solution) => console.log("Resolved with " + solution)); // Resolved with 42
實åä¸ï¼ä½¿ç¨ catch
ææè¢«å¦æ±ºç promise è¼çæ³çï¼èä¸å»ºè°ä½¿ç¨å
©å弿¸ then
èªæ³ï¼å¦ä¸å±ç¤ºã
Promise.resolve()
.then(() => {
// Makes .then() return a rejected promise
throw "Oh no!";
})
.catch((reason) => {
console.error("onRejected function called: ", reason);
})
.then(() => {
console.log("I am always called even if the prior then's promise rejects");
});
ä½ ä¹å¯ä»¥éé串æ¥å¯¦ä½ä¸å Promise-based API å½å¼ï¼åºæ¼å®æ¬èº«ã
function fetch_current_data() {
// The fetch() API returns a Promise. This function
// exposes a similar API, except the fulfillment
// value of this function's Promise has had more
// work done on it.
return fetch("current-data.json").then((response) => {
if (response.headers.get("content-type") != "application/json") {
throw new TypeError();
}
var j = response.json();
// maybe do something with j
return j; // fulfillment value given to user of
// fetch_current_data().then()
});
}
è¥ onFulfilled
åå³ä¸å promiseï¼å then
ç實ç¾/妿±ºå°å決å®ã
function resolveLater(resolve, reject) {
setTimeout(function () {
resolve(10);
}, 1000);
}
function rejectLater(resolve, reject) {
setTimeout(function () {
reject(20);
}, 1000);
}
var p1 = Promise.resolve("foo");
var p2 = p1.then(function () {
// Return promise here, that will be resolved to 10 after 1 second
return new Promise(resolveLater);
});
p2.then(
function (v) {
console.log("resolved", v); // "resolved", 10
},
function (e) {
// not called
console.log("rejected", e);
},
);
var p3 = p1.then(function () {
// Return promise here, that will be rejected with 20 after 1 second
return new Promise(rejectLater);
});
p3.then(
function (v) {
// not called
console.log("resolved", v);
},
function (e) {
console.log("rejected", e); // "rejected", 20
},
);
è¦ç¯ ç覽å¨ç¸å®¹æ§ åè¦
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4