Answer the question
In order to leave comments, you need to log in
How to catch an exception in a JS Promise without assigning a separate variable?
Is it possible in JS to catch an exception in a promise callback without assigning a separate variable? Let me explain what it means:
//создаём промис со статусом resolved
var promise = new Promise(function(resolve) {
resolve('Hello, world');
});
//навешиваем коллбэк, в котором вызываем эксепшн
promise.then(function () {
throw 123;
});
//сюда эксепшн не попадёт, так как promise находится в статусе resolved
promise.catch(function (error) {
console.error('Exception: ', error);
});
var promise = new Promise(function(resolve) {
resolve('Hello, world');
});
//присваиваем переменной other значение нового промиса
var other = promise.then(function() {
throw 123;
});
//в этом случае эксепшн пойман
other.catch(function(error) {
console.error('Exception: ', error);
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question