Baseline Widely available
catch() æ¹æ³åªèç Promise ç被æçµçæ
ï¼ä¸¦åå³ä¸åæ°ç Promise
ç©ä»¶ãæ¤æ¹æ³çè¡çºçåæ¼å¼å« Promise.prototype.then(undefined, onRejected)
ã
p.catch(onRejected);
p.catch(function (reason) {
// rejection
});
忏
ä¸å Function
ï¼å¨ Promise
被æçµæè¢«å¼å«ãéåå½å¼æä¸å弿¸ï¼
reason
失æè¨æ¯ã
è¥ onRejected æåºä¸åé¯èª¤æåå³ä¸å被æçµç Promiseï¼å catch() åå³ç Promise 被æçµï¼å ¶ä»æ 形齿¯è¢«å¯¦ç¾ã
å¼å«ï¼catch
ç promiseï¼ç©ä»¶ï¼å
§é¨å¼å« Promise.prototype.then
ï¼å³å
¥å¼æ¸ undefined å onRejectedï¼æ¥è以ä¹çµæåå³ï¼çµæçº Promise
ï¼ã
å §é¨å¼å«æ¼ç¤ºï¼
// overriding original Promise.prototype.then/catch just to add some logs
(function (Promise) {
var originalThen = Promise.prototype.then;
var originalCatch = Promise.prototype.catch;
Promise.prototype.then = function () {
console.log(
"> > > > > > called .then on %o with arguments: %o",
this,
arguments,
);
return originalThen.apply(this, arguments);
};
Promise.prototype.catch = function () {
console.log(
"> > > > > > called .catch on %o with arguments: %o",
this,
arguments,
);
return originalCatch.apply(this, arguments);
};
})(this.Promise);
// calling catch on an already resolved promise
Promise.resolve().catch(function XXX() {});
// logs:
// > > > > > > called .catch on Promise{} with arguments: Arguments{1} [0: function XXX()]
// > > > > > > called .then on Promise{} with arguments: Arguments{2} [0: undefined, 1: function XXX()]
æè¿°
catch
æ¹æ³å¨èç promise çµåçé¯èª¤æå¾æå¹«å©ã
catch
æ¹æ³
var p1 = new Promise(function (resolve, reject) {
resolve("Success");
});
p1.then(function (value) {
console.log(value); // "Success!"
throw "oh, no!";
})
.catch(function (e) {
console.log(e); // "oh, no!"
})
.then(
function () {
console.log("after a catch the chain is restored");
},
function () {
console.log("Not fired due to the catch");
},
);
// The following behaves the same as above
p1.then(function (value) {
console.log(value); // "Success!"
return Promise.reject("oh, no!");
})
.catch(function (e) {
console.log(e); // "oh, no!"
})
.then(
function () {
console.log("after a catch the chain is restored");
},
function () {
console.log("Not fired due to the catch");
},
);
æåºä¾å¤æçé·é±
// Throwing an error will call the catch method most of the time
var p1 = new Promise(function (resolve, reject) {
throw "Uh-oh!";
});
p1.catch(function (e) {
console.log(e); // "Uh-oh!"
});
// Errors thrown inside asynchronous functions will act like uncaught errors
var p2 = new Promise(function (resolve, reject) {
setTimeout(function () {
throw "Uncaught Exception!";
}, 1000);
});
p2.catch(function (e) {
console.log(e); // This is never called
});
// Errors thrown after resolve is called will be silenced
var p3 = new Promise(function (resolve, reject) {
resolve();
throw "Silenced Exception!";
});
p3.catch(function (e) {
console.log(e); // This is never called
});
妿 Promise 被實ç¾
//Create a promise which would not call onReject
var p1 = Promise.resolve("calling next");
var p2 = p1.catch(function (reason) {
//This is never called
console.log("catch p1!");
console.log(reason);
});
p2.then(
function (value) {
console.log("next promise's onFulfilled"); /* next promise's onFulfilled */
console.log(value); /* calling next */
},
function (reason) {
console.log("next promise's onRejected");
console.log(reason);
},
);
è¦ç¯ ç覽å¨ç¸å®¹æ§ åè¦
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