Baseline Widely available
then()
㯠Promise
ã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã½ããã§ãããæå¤§ 2 ã¤ã®å¼æ°ã¨ãã¦ããã® Promise
ãæåããå ´åã¨å¤±æããå ´åã®ã³ã¼ã«ããã¯é¢æ°ãåãã¾ããã³ã¼ã«ããã¯ã¯ããããå¼ã³åºããããããã¹å
ã«æ ¼ç´ãããããã«å¥ã® Promise
ãªãã¸ã§ã¯ããè¿å¤ã«ããã¦è¿ããä»ã®ãããã¹ã®ã¡ã½ããã«å¯¾ããé£éå¼ã³åºããè¡ããã¨ãã§ãã¾ãã
const promise1 = new Promise((resolve, reject) => {
resolve("Success!");
});
promise1.then((value) => {
console.log(value);
// Expected output: "Success!"
});
æ§æ
then(onFulfilled)
then(onFulfilled, onRejected)
弿°
onFulfilled
ãã®ãããã¹ãå±¥è¡ãããã¨ãã«éåæã«å¼ã³åºããã颿°ã§ãããã®è¿å¤ã¯ãthen()
ããè¿ããããããã¹ã®å±¥è¡å¤ã«ãªãã¾ãããã®é¢æ°ã¯ä»¥ä¸ã®å¼æ°ã¨å
±ã«å¼ã³åºããã¾ãã
value
ãã®ãããã¹ãå±¥è¡ãããã¨ãã®å¤ã
ããã颿°ã§ã¯ãªãå ´åã¯ãå
é¨çã«ãå±¥è¡ãããå¤ãéãããã® æç 颿° ((x) => x
) ã«ç½®ãæãããã¾ãã
onRejected
çç¥å¯
ãã®ãããã¹ãæå¦ãããã¨ãã«éåæã«å¼ã³åºããã颿°ã§ãããã®è¿å¤ã¯ãthen()
ããè¿ããããããã¹ã®å±¥è¡å¤ã«ãªãã¾ãããã®é¢æ°ã¯ä»¥ä¸ã®å¼æ°ã¨å
±ã«å¼ã³åºããã¾ãã
reason
ããããã¹ãæå¦ãããã¨ãã®å¤ã
ããã颿°ã§ã¯ãªãå ´åã¯ãå
é¨çã«å¼æ°ã¨ãã¦åãåã£ãã¨ã©ã¼ãæãã ã¹ãã¯ã¼ 颿° ((x) => { throw x; }
) ã«ç½®ãæãããã¾ãã
æ°ãã Promise
ãããã«è¿ãã¾ãããã®æ°ãããããã¹ã¯ãç¾å¨ã®ãããã¹ã®ç¶æ
ã«é¢ä¿ãªããè¿ãã¨ãã«ã¯å¸¸ã«å¾
æ©ç¶æ
ã§ãã
onFulfilled
㨠onRejected
ãã³ãã©ã¼ã®ãããããå®è¡ãããç¾å¨ã®ãããã¹ã®å±¥è¡ããããæå¦ãããããå¦çããã¾ãããã®å¼ã³åºãã¯ãç¾å¨ã®ãããã¹ãæ¢ã«æ±ºå®ããã¦ããå ´åã§ãã常ã«éåæã§è¡ããã¾ããè¿ããããããã¹ï¼p
ã¨å¼ã¶ï¼ã®æ¯ãèãã¯ããã³ãã©ã¼ã®å®è¡çµæã«ä¾åããä¸é£ã®ç¹å®ã®ã«ã¼ã«ã«å¾ãã¾ãããããã³ãã©ã¼é¢æ°ãã
p
ã¯è¿å¤ããã®å¤ã¨ãã¦å±¥è¡ããã¾ããp
㯠undefined
ããã®å¤ã¨ãã¦å±¥è¡ããã¾ããp
ã¯ãã®å¤ã¨ãã¦ã¨ã©ã¼ãæãã¦æå¦ããã¾ããp
ã¯ããã®ãããã¹ã®å¤ããã®å¤ã¨ãã¦å±¥è¡ããã¾ããp
ã¯ããã®ãããã¹ã®å¤ããã®å¤ã¨ãã¦æå¦ããã¾ããp
ã¯å¾
æ©ä¸ã¨ãªãããã®ãããã¹ãå±¥è¡/æå¦ãããç´å¾ã«ãã®ãããã¹ã®å¤ã¨ãã¦å±¥è¡/æå¦ããããããã¹ã¨ãªãã¾ããthen()
ã¡ã½ããã¯ããããã¹ã®æçµçãªå®äºï¼å±¥è¡ããããæå¦ããããï¼ã®ããã«ã³ã¼ã«ããã¯é¢æ°ãã¹ã±ã¸ã¥ã¼ã«ãã¾ããããã¯ãããã¹ã®åºæ¬ã¡ã½ããã§ããThenable ãããã³ã«ã¯ãã¹ã¦ã®ãããã¹åãªãã¸ã§ã¯ãã then()
ã¡ã½ãããå
¬éãããã¨ãæ³å®ãã¦ãããcatch()
㨠finally()
ã¡ã½ããã¯ãã©ã¡ãããªãã¸ã§ã¯ãã® then()
ã¡ã½ãããå¼ã³åºããã¨ã§åä½ããããã«ãªã£ã¦ãã¾ãã
onRejected
ãã³ãã©ã¼ã®è©³ç´°ã«ã¤ãã¦ã¯ã catch()
ã®ãªãã¡ã¬ã³ã¹ãåç
§ãã¦ãã ããã
then()
ã¯æ°ãããããã¹ãªãã¸ã§ã¯ããè¿ãã¾ãããå¼ã³åºããããããã¹ãªãã¸ã§ã¯ãã夿´ãããã³ãã©ã¼ãå
é¨ãªã¹ãã«è¿½å ãã¾ãã ãããã£ã¦ããã³ãã©ã¼ã¯å
ã®ãããã¹ã«ãã£ã¦ä¿æããããã®å¯¿å½ã¯å°ãªãã¨ãå
ã®ãããã¹ã®å¯¿å½ã¨åãé·ãã«ãªãã¾ãã ä¾ãã°ã次ã®ä¾ã§ã¯ãè¿ããããããã¹ãä¿æãããªãã«ãããããããæçµçã«ã¯ã¡ã¢ãªã¼ã使ãæããã¾ãã
const pendingPromise = new Promise(() => {});
while (true) {
pendingPromise.then(doSomething);
}
then()
ã¯ãæ°ãããããã¹ãªãã¸ã§ã¯ããè¿ããã¨ã§ãã åããããã¹ãªãã¸ã§ã¯ãã§ then()
ã¡ã½ããã 2 åå¼ã³åºãã¨ï¼é£éããã®ã§ã¯ãªãï¼ããã®ãããã¹ãªãã¸ã§ã¯ã㯠2 çµã®æ±ºå®ãã³ãã©ã¼ãä¿æãããã¨ã«ãªãã¾ããåããããã¹ãªãã¸ã§ã¯ãã«ä»ãããããã¹ã¦ã®ãã³ãã©ã¼ã¯ã常ã«è¿½å ãããé çªã«å¼ã³åºããã¾ããããã«ã then()
ã®åå¼ã³åºãã«ãã£ã¦è¿ããã 2 ã¤ã®ãããã¹ã¯ãå¥åã®é£éãå§ãããäºãã®æ±ºå®ãå¾
ã¤ãã¨ã¯ããã¾ããã
then()
ãã§ã¼ã³ã«æ²¿ã£ã¦çºçãã Thenable ãªãã¸ã§ã¯ãã¯å¸¸ã«è§£æ±ºããã¾ããonFulfilled
ãã³ãã©ã¼ã¯ thenable ãªãã¸ã§ã¯ããæ±ºãã¦åãåãããããããã®ãã³ãã©ã¼ã«ãã£ã¦è¿ããã thenable ã¯å¸¸ã«æ¬¡ã®ãã³ãã©ã¼ã«æ¸¡ãããåã«è§£æ±ºããã¾ããããã¯ãæ°ãããããã¹ãæ§ç¯ããã¨ãã«ã executor
ããæ¸¡ããã resolve
颿°ã¨ reject
颿°ãä¿åãããç¾å¨ã®ãããã¹ã決å®ããã¨ãã«ãããããã®é¢æ°ãå±¥è¡ãããå¤ã¾ãã¯æçµ¶ã®çç±ã¨ã¨ãã«å¼ã³åºãããããã§ãã解決ãã¸ãã¯ã¯ã Promise()
ã³ã³ã¹ãã©ã¯ã¿ã¼ããæ¸¡ããããªã¾ã«ãã¼é¢æ°ããæ±ºã¾ãã¾ãã
then()
ã¯ãµãã¯ã©ã¹åã«å¯¾å¿ãã¦ãããPromise
ã®ãµãã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã«å¯¾ãã¦å¼ã³åºããã¨ãã§ãããã®çµæã¯ãµãã¯ã©ã¹ã®åã®ãããã¹ã«ãªãã¾ããè¿ãå¤ã®ç¨®é¡ã¯ [Symbol.species]
ããããã£ã§ã«ã¹ã¿ãã¤ãºãããã¨ãã§ãã¾ãã
const p1 = new Promise((resolve, reject) => {
resolve("Success!");
// or
// reject(new Error("Error!"));
});
p1.then(
(value) => {
console.log(value); // Success!
},
(reason) => {
console.error(reason); // Error!
},
);
ã©ã¡ããã®å¼æ°ã«é¢æ°ä»¥å¤ãæå®
Promise.resolve(1).then(2).then(console.log); // 1
Promise.reject(1).then(2, 2).then(console.log, console.log); // 1
é£é
then
ã¡ã½ãã㯠Promise
ãè¿ãã®ã§ãã¡ã½ããé£éãã§ãã¾ãã
then
ã«ãã³ãã©ã¼ã¨ãã¦æ¸¡ããã颿°ã Promise
ãè¿ãå ´åãåã Promise
ãã¡ã½ããé£éã®æ¬¡ã® then
ã«ç¾ãã¾ããæ¬¡ã®ã¹ããããã¯ãéåæå®è¡ãã·ãã¥ã¬ã¼ãããã setTimeout
颿°ä»ãã®ã³ã¼ãã§ãã
Promise.resolve("foo")
// 1. Receive "foo", concatenate "bar" to it, and resolve that to the next then
.then(
(string) =>
new Promise((resolve, reject) => {
setTimeout(() => {
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 returning the unworked on
// string to the next then
.then((string) => {
setTimeout(() => {
string += "baz";
console.log(string); // foobarbaz
}, 1);
return string;
})
// 3. print helpful messages about how the code in this section will be run
// before the string is actually processed by the mocked asynchronous code in the
// previous then block.
.then((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); // foobar
});
// Logs, in order:
// Last Then: oops... didn't bother to instantiate and return a promise in the prior then so the sequence may be a bit surprising
// foobar
// foobarbaz
then()
ããè¿ãããå¤ã¯ãPromise.resolve()
ã¨åãæ¹æ³ã§è§£æ±ºããã¾ããã¤ã¾ããThenable ãªãã¸ã§ã¯ãã対å¿ãã¦ãã¦ãè¿å¤ããããã¹ã§ãªãå ´åã¯ãæé»ã®ãã¡ã« Promise
ã§ã©ããããããã®å¾è§£æ±ºããã¾ãã
const p2 = new Promise((resolve, reject) => {
resolve(1);
});
p2.then((value) => {
console.log(value); // 1
return value + 1;
}).then((value) => {
console.log(value, "- A synchronous value works"); // 2 - A synchronous value works
});
p2.then((value) => {
console.log(value); // 1
});
then
ã®å¼æ°ã¨ãã¦æ¸¡ãã颿°ãæå¦ããããããã¹ãè¿ããå ´åããä¾å¤ï¼ã¨ã©ã¼ï¼ãçºçããå ´åã¯ãæå¦ããããããã¹ãè¿ãã¾ãã
Promise.resolve()
.then(() => {
// Makes .then() return a rejected promise
throw new Error("Oh no!");
})
.then(
() => {
console.log("Not called.");
},
(error) => {
console.error(`onRejected function called: ${error.message}`);
},
);
ç¾å®çã«ã¯ãcatch()
ã使ã£ã¦æå¦ããããããã¹ãææããæ¹ãã then()
ã® 2 ã¤ã®å¼æ°ã®æ§æã使ã£ã¦å¦çãããããç¾å®çã§ããä¸è¨ã®ä¾ãè¦ã¦ãã ããã
Promise.resolve()
.then(() => {
// Makes .then() return a rejected promise
throw new Error("Oh no!");
})
.catch((error) => {
console.error(`onRejected function called: ${error.message}`);
})
.then(() => {
console.log("I am always called even if the prior then's promise rejects");
});
ä»ã«ããã¹ã¦ã®å ´åã«ããã¦ãè¿ããããããã¹ã¯æçµçã«å±¥è¡ããã¾ãã以ä¸ã®ä¾ã§ã¯ãæåã® then()
ã¯ããã§ã¼ã³å
ã®åã®ãããã¹ãæå¦ãããã«ãããããããå±¥è¡ããããããã¹ã«å
ã¾ãã 42
ãè¿ãã¾ãã
Promise.reject()
.then(
() => 99,
() => 42,
) // onRejected ã¯å±¥è¡ããããããã¹ã«ã©ããããã 42 ãè¿ã
.then((solution) => console.log(`Resolved with ${solution}`)); // Fulfilled with 42
ãã onFulfilled
ããããã¹ãè¿ããå ´åã then
ã®è¿å¤ã¯ãã®ãããã¹ã®æçµçãªç¶æ
ã«åºã¥ãã¦å±¥è¡ããã/æå¦ããããã¨ã«ãªãã¾ãã
function resolveLater(resolve, reject) {
setTimeout(() => {
resolve(10);
}, 1000);
}
function rejectLater(resolve, reject) {
setTimeout(() => {
reject(new Error("Error"));
}, 1000);
}
const p1 = Promise.resolve("foo");
const p2 = p1.then(() => {
// Return promise here, that will be resolved to 10 after 1 second
return new Promise(resolveLater);
});
p2.then(
(v) => {
console.log("resolved", v); // "resolved", 10
},
(e) => {
// not called
console.error("rejected", e);
},
);
const p3 = p1.then(() => {
// Return promise here, that will be rejected with 'Error' after 1 second
return new Promise(rejectLater);
});
p3.then(
(v) => {
// not called
console.log("resolved", v);
},
(e) => {
console.error("rejected", e); // "rejected", 'Error'
},
);
ãããã¹ãã¼ã¹ã® API ã使ç¨ãã颿°ãå¥ã®é¢æ°ã®ä¸ã«å®è£ ããããã«ããã§ã¼ãã³ã°ã使ç¨ãããã¨ãã§ãã¾ãã
function fetchCurrentData() {
// fetch() API ã¯ãããã¹ãè¿ãã¾ãããã®é¢æ°ã¯
// åæ§ã®APIãå
¬éãã¦ãã¾ããããã®é¢æ°ã®
// ãããã¹ã®å±¥è¡ãããå¤ã«ã¯ãããå¤ãã®ä½æ¥ã
// æ½ããã¦ãããã¨ãç¹å¾´ã§ãã
return fetch("current-data.json").then((response) => {
if (response.headers.get("content-type") !== "application/json") {
throw new TypeError();
}
const j = response.json();
// ãããã j ã§ä½ãããã
// fulfillment value given to user of
// fetchCurrentData().then()
return j;
});
}
then() ã®éåææ§
以ä¸ã¯ then
ã¡ã½ããã®éåææ§ã示ãä¾ã§ããã
// ä¾ã¨ãã¦è§£æ±ºæ¸ã¿ã®ãããã¹ 'resolvedProm' ã使ç¨ããã¨ã
// 颿°å¼ã³åºã 'resolvedProm.then(...)' ã¯ç´ã¡ã«æ°ãããããã¹ãè¿ãã¾ããã
// ãã®ãã³ãã©ã¼ '(value) => {...}' 㯠console.log ã§ç¤ºãããããã«éåæã«å¼ã°ãããã¨ã«ãªãã¾ãã
// æ°ãããããã¹ã¯ 'thenProm' ã«ä»£å
¥ããã
// thenProm ã¯ãã³ãã©ã¼ãè¿ãå¤ã§è§£æ±ºããã¾ãã
const resolvedProm = Promise.resolve(33);
console.log(resolvedProm);
const thenProm = resolvedProm.then((value) => {
console.log(
`this gets called after the end of the main stack. the value received is: ${value}, the value returned is: ${
value + 1
}`,
);
return value + 1;
});
console.log(thenProm);
// setTimeout ã使ç¨ããã¨ãã¹ã¿ãã¯ã空ã«ãªã£ãç¬éã¾ã§é¢æ°ã®å®è¡ãå»¶æãããã¨ãã§ãã
setTimeout(() => {
console.log(thenProm);
});
// Logs, in order:
// Promise {[[PromiseStatus]]: "resolved", [[PromiseResult]]: 33}
// Promise {[[PromiseStatus]]: "pending", [[PromiseResult]]: undefined}
// "this gets called after the end of the main stack. the value received is: 33, the value returned is: 34"
// Promise {[[PromiseStatus]]: "resolved", [[PromiseResult]]: 34}
仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
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