S
S
sacryfice2015-11-12 00:25:31
JavaScript
sacryfice, 2015-11-12 00:25:31

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);
});

The following structure could solve the problem:
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);
});

The problem is that the promise in my case will be created in an external library and there is no way to return the entire chain of primis. That is, I would like to track this chain from the initial promise. Is there such a possibility?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-11-12
@sacryfice

there is no way to return the entire chain of primis

How so? Always return the entire chain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question