Baseline Widely available
then()
ë©ìëë Promise
를 리í´íê³ ë ê°ì ì½ë°± í¨ì를 ì¸ìë¡ ë°ìµëë¤. íëë Promise
ê° ì´ííì ë, ë¤ë¥¸ íëë ê±°ë¶íì ë를 ìí ì½ë°± í¨ìì
ëë¤.
const promise1 = new Promise((resolve, reject) => {
resolve("Success!");
});
promise1.then((value) => {
console.log(value);
// Expected output: "Success!"
});
ì°¸ê³ : 매ê°ë³ì ì¤ íë ì´ìì ìëµíê±°ë í¨ìê° ìë ê°ì ì ë¬í ê²½ì°, then
ì í¸ë¤ë¬ê° ìë ê²ì´ ëì§ë§ ì¤ë¥ë¥¼ ë°ìíì§ë ììµëë¤. then
ë°ë¡ ì´ì ì Promise
ê° then
ì í¸ë¤ë¬ê° ìë ìíë¡ ìë£(ì´íì´ë ê±°ë¶)íì ê²½ì°, ì¶ê° í¸ë¤ë¬ê° ìë Promise
ê° ìì±ëë©°, ìë Promise
ì ë§ì§ë§ ìí를 ê·¸ëë¡ ë¬¼ë ¤ë°ìµëë¤.
p.then(onFulfilled, onRejected);
p.then(function(value) {
// ì´í
}, function(reason) {
// ê±°ë¶
});
매ê°ë³ì
onFulfilled
Promise
ê° ìíë ë í¸ì¶ëë Function
ì¼ë¡, ì´í ê°(fulfillment value) íë를 ì¸ìë¡ ë°ìµëë¤.
onRejected
Promise
ê° ê±°ë¶ë ë í¸ì¶ëë Function
ì¼ë¡, ê±°ë¶ ì´ì (rejection reason) íë를 ì¸ìë¡ ë°ìµëë¤.
Promise
ê° ì´ííê±°ë ê±°ë¶íì ë, ê°ê°ì í´ë¹íë í¸ë¤ë¬ í¨ì(onFulfilled
ë onRejected
)ê° ë¹ë기ì ì¼ë¡ ì¤íë©ëë¤. í¸ë¤ë¬ í¨ìë ë¤ì ê·ì¹ì ë°ë¼ ì¤íë©ëë¤.
then
ìì ë°íí íë¡ë¯¸ì¤ë ê·¸ ë°íê°ì ìì ì ê²°ê³¼ê°ì¼ë¡ íì¬ ì´íí©ëë¤.then
ìì ë°íí íë¡ë¯¸ì¤ë undefined
를 ê²°ê³¼ê°ì¼ë¡ íì¬ ì´íí©ëë¤.then
ìì ë°íí íë¡ë¯¸ì¤ë ê·¸ ì¤ë¥ë¥¼ ìì ì ê²°ê³¼ê°ì¼ë¡ íì¬ ê±°ë¶í©ëë¤.then
ìì ë°íí íë¡ë¯¸ì¤ë ê·¸ íë¡ë¯¸ì¤ì ê²°ê³¼ê°ì ìì ì ê²°ê³¼ê°ì¼ë¡ íì¬ ì´íí©ëë¤.then
ìì ë°íí íë¡ë¯¸ì¤ë ê·¸ íë¡ë¯¸ì¤ì ê²°ê³¼ê°ì ìì ì ê²°ê³¼ê°ì¼ë¡ íì¬ ê±°ë¶í©ëë¤.then
ìì ë°íí íë¡ë¯¸ì¤ë ê·¸ íë¡ë¯¸ì¤ì ì´í ì¬ë¶ì ê²°ê³¼ê°ì ë°ë¦
ëë¤.ë¤ì ìì ìì then
ë©ìëì ë¹ë기ì±ì íì¸í ì ììµëë¤.
// ì´íí íë¡ë¯¸ì¤ë¥¼ ë°ì¼ë©´ 'then' ë¸ë¡ë ë°ë¡ ì¤íëì§ë§,
// í¸ë¤ë¬ë ìë console.logììì ê°ì´ ë¹ë기ì ì¼ë¡ ì¤íë¨
const resolvedProm = Promise.resolve(33);
let thenProm = resolvedProm.then((value) => {
console.log(
"ì´ ë¶ë¶ì í¸ì¶ ì¤í ì´íì ì¤íë©ëë¤. ì ë¬ë°ì ê°ì´ì ë°íê°ì " +
value +
"ì
ëë¤.",
);
return value;
});
// thenPromì ê°ì ì¦ì 기ë¡
console.log(thenProm);
// setTimeoutì¼ë¡ í¨ì ì¤íì í¸ì¶ ì¤íì´ ë¹ ëê¹ì§ 미룰 ì ìì
setTimeout(() => {
console.log(thenProm);
});
// ë¡ê·¸ ì¶ë ¥ ê²°ê³¼ (ììëë¡):
// Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
// "ì´ ë¶ë¶ì í¸ì¶ ì¤í ì´íì ì¤íë©ëë¤. ì ë¬ë°ì ê°ì´ì ë°íê°ì 33ì
ëë¤."
// Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: 33}
ì¤ëª
then
ê³¼ Promise.prototype.catch()
ë©ìëë íë¡ë¯¸ì¤ë¥¼ ë°íí기 ë문ì, ì²´ì´ëì´ ê°ë¥í©ëë¤. ì´ë¥¼ í©ì±ì´ë¼ê³ ë í©ëë¤.
then
ë©ìë ì¬ì©
var p1 = new Promise(function (resolve, reject) {
resolve("ì±ê³µ!");
// ëë
// reject("ì¤ë¥!");
});
p1.then(
function (value) {
console.log(value); // ì±ê³µ!
},
function (reason) {
console.log(reason); // ì¤ë¥!
},
);
ì²´ì´ë
then
ë©ìëë Promise
를 리í´í기 ë문ì, ì´ì´ì§ë then
í¸ì¶ë¤ì ìì½ê² ì°ê²°í ì ììµëë¤.
then
ì í¸ë¤ë¬ë¡ ì ë¬ë í¨ìê° íë¡ë¯¸ì¤ë¥¼ ë°íí ê²½ì°, ì´ì ëë±í íë¡ë¯¸ì¤ê° ë©ìë ì²´ì¸ì ê·¸ë¤ì then
ì ë
¸ì¶ë©ëë¤. ìë ìì ììë setTimeout
í¨ìë¡ ë¹ë기 ì½ë를 íë´ë
ëë¤.
Promise.resolve("foo")
// 1. "foo"를 ë°ê³ "bar"를 ì¶ê°í ë¤ì ê·¸ ê°ì¼ë¡ ì´ííì¬ ë¤ì thenì ë겨ì¤
.then(function (string) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
string += "bar";
resolve(string);
}, 1);
});
})
// 2. "foobar"를 ë°ê³ ê·¸ëë¡ ë¤ì thenì ëê²¨ì¤ ë¤,
// ëì¤ì ì½ë°± í¨ììì ê°ê³µíê³ ì½ìì ì¶ë ¥
.then(function (string) {
setTimeout(function () {
string += "baz";
console.log(string);
}, 1);
return string;
})
// 3. ì´ ë¶ë¶ì ì½ëë ì´ì ì then ë¸ë¡ ìì (ê°ì§) ë¹ë기 ì½ëìì
// ì¤ì ë¡ ë¬¸ìì´ì ê°ê³µí기 ì ì ì¤íë¨
.then(function (string) {
console.log(
"ë§ì§ë§ Then: ì... ë°©ê¸ thenìì íë¡ë¯¸ì¤ ë§ë¤ê³ ë°ííë 걸 ê¹ë¨¹ì´ì " +
"ì¶ë ¥ ììê° ì¢ ì´ìí ì§ë 몰ë¼ì",
);
// 'baz' ë¶ë¶ì setTimeout í¨ìë¡ ë¹ë기ì ì¼ë¡ ì¤íë기 ë문ì
// ì´ê³³ì stringìë ìì§ 'baz' ë¶ë¶ì´ ìì
console.log(string);
});
// ë¡ê·¸ ì¶ë ¥ ê²°ê³¼ (ììëë¡):
// ë§ì§ë§ Then: ì... ë°©ê¸ thenìì íë¡ë¯¸ì¤ ë§ë¤ê³ ë°ííë 걸 ê¹ë¨¹ì´ì ì¶ë ¥ ììê° ì¢ ì´ìí ì§ë 몰ë¼ì
// foobar
// foobarbaz
then
í¸ë¤ë¬ìì ê°ì ê·¸ëë¡ ë°íí ê²½ì°ìë Promise.resolve(<í¸ë¤ë¬ìì ë°íí ê°>)
ì ë°ííë ê²ê³¼ ê°ìµëë¤.
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 + " - ë기ì ì¼ë¡ ì§ë ëìê°");
});
p2.then(function (value) {
console.log(value); // 1
});
í¨ììì ì¤ë¥ê° ë°ìíê±°ë ê±°ë¶í íë¡ë¯¸ì¤ë¥¼ ë°íí ê²½ì° then
ììë ê±°ë¶í íë¡ë¯¸ì¤ë¥¼ ë°íí©ëë¤.
Promise.resolve()
.then(() => {
// .then()ìì ê±°ë¶í íë¡ë¯¸ì¤ë¥¼ ë°íí¨
throw new Error("ì¼ì
!");
})
.then(
() => {
console.log("ì¤íëì§ ìë ì½ë");
},
(error) => {
console.error("onRejected í¨ìê° ì¤íë¨: " + error.message);
},
);
ì´ì¸ì 모ë ê²½ì°ìë ê³§ ì´íí (ë¹ë기ì ì¼ë¡ ì¤íëë) íë¡ë¯¸ì¤ë¥¼ ë°íí©ëë¤. ë¤ì ìì ììë ë°ë¡ ì´ì ì íë¡ë¯¸ì¤ê° ê±°ë¶íììë 첫 ë²ì§¸ then
ììë 42
ì ê°ì ê°ë ê³§ ì´íí íë¡ë¯¸ì¤ë¥¼ ë°íí©ëë¤.
Promise.reject()
.then(
() => 99,
() => 42,
) // onRejectedììë 42를 ê³§ ì´íí íë¡ë¯¸ì¤ë¡ ê°ì¸ì ë°íí¨
.then((solution) => console.log(solution + "ë¡ ì´íí¨")); // 42ë¡ ì´íí¨
ì¤ì ê°ë° ììë ìëì ê°ì´ ê±°ë¶í íë¡ë¯¸ì¤ë¥¼ then
ì 2ë¨ í¸ë¤ë¬ë³´ë¤ë catch
를 ì¬ì©í´ ì²ë¦¬íë ê²½ì°ê° ë§ìµëë¤.
Promise.resolve()
.then(() => {
// .then()ìì ê±°ë¶í íë¡ë¯¸ì¤ë¥¼ ë°íí¨
throw new Error("ì¼ì
!");
})
.catch((error) => {
console.error("onRejected í¨ìê° ì¤íë¨: " + error.message);
})
.then(() => {
console.log("ì²ì thenì íë¡ë¯¸ì¤ê° ê±°ë¶íì§ë§ ê·¸ëë ì´ ì½ëë ì¤íë¨");
});
ì²´ì´ëì ì´ì©í´ íë¡ë¯¸ì¤ ê¸°ë° í¨ì ìì ë¤ë¥¸ íë¡ë¯¸ì¤ ê¸°ë° í¨ì를 구íí ìë ììµëë¤.
function fetch_current_data() {
// fetch() APIë íë¡ë¯¸ì¤ë¥¼ ë°íí©ëë¤. ì´ í¨ìë
// APIê° ë¹ì·íì§ë§, ì´ í¨ìì íë¡ë¯¸ì¤ë
// ì¶ê° ìì
ì ê±°ì¹ ì´íì ì´íê°ì ë°íí©ëë¤.
return fetch("current-data.json").then((response) => {
if (response.headers.get("content-type") != "application/json") {
throw new TypeError();
}
var j = response.json();
// j ê°ê³µí기
return j; // fetch_current_data().then()ì íµí´
// ì´íê°ì ì¬ì©í ì ìì
});
}
onFulfilled
ê° íë¡ë¯¸ì¤ë¥¼ ë°íí ê²½ì°, then
ì ë°íê° ìì ê·¸ íë¡ë¯¸ì¤ì ìí´ ì´í/ê±°ë¶í©ëë¤.
function resolveLater(resolve, reject) {
setTimeout(function () {
resolve(10);
}, 1000);
}
function rejectLater(resolve, reject) {
setTimeout(function () {
reject(new Error("ì¤ë¥"));
}, 1000);
}
var p1 = Promise.resolve("foo");
var p2 = p1.then(function () {
// 1ì´ ë¤ì 10ì¼ë¡ ì´íí íë¡ë¯¸ì¤ ë°í
return new Promise(resolveLater);
});
p2.then(
function (v) {
console.log("ì´í", v); // "ì´í", 10
},
function (e) {
// ì¤íëì§ ìì
console.log("ê±°ë¶", e);
},
);
var p3 = p1.then(function () {
// 1ì´ ë¤ì 'ì¤ë¥'ë¡ ê±°ë¶í íë¡ë¯¸ì¤ ë°í
return new Promise(rejectLater);
});
p3.then(
function (v) {
// ì¤íëì§ ìì
console.log("ì´í", v);
},
function (e) {
console.log("ê±°ë¶", e); // "ê±°ë¶", 'ì¤ë¥'
},
);
window.setImmediate
ì íë¡ë¯¸ì¤ ê¸°ë° í´ë¦¬í
Function.prototype.bind()
Reflect.apply
(Reflect.apply()
) ë©ìë를 ì¬ì©íì¬ (ì·¨ìí ì ìë) setImmedateì ë¹ì·í í¨ì를 ìì±í©ëë¤.
const nextTick = (() => {
const noop = () => {}; // literally
const nextTickPromise = () => Promise.resolve().then(noop);
const rfab = Reflect.apply.bind; // (thisArg, fn, thisArg, [...args])
const nextTick = (fn, ...args) => (
fn !== undefined
? Promise.resolve(args).then(rfab(null, fn, null))
: nextTickPromise(),
undefined
);
nextTick.ntp = nextTickPromise;
return nextTick;
})();
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ì°¸ì¡°
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