Stop the promise chain halfway

Hey guys, I'm trying to stop the promise chain halfway (after the catch). Therefore, after an error occurred in the first promise, catching it will catch it, but I do not want the chain to continue. I use a blue bird. How should I do it?

getRedirectedURL(url).then(function(url) {
                console.log(1);
                url = domainCleanse(url);
                sql = mysql.format(select, url);

                return [ url, mysqlQuery(sql) ];

            }).catch(function(error) {
                console.log(2);
                console.error(error);
                socket.emit('error:unreachable', url + ' was unreachable');
            }).spread(function(url, rows) {
                console.log(3);
                if(_.isEmpty(rows[0])) {
                    socketList.push({
                        url: url,
                        ttl: _.now(),
                        socket: socket,
                        added: false
                    });
                } else {
                    socket.emit('done', mapResults(rows[0]));
                }
            }).catch(function(error) {
                console.log(4);
                console.error(error);
                socket.emit('error', 'We could not reach ' + url + ' at this time.');
            });
+4
source share
1 answer

Summarizing your example, it looks like this:

promiseToFoo()
    .then(promiseToBar)
    .catch(failedToFooOrBar)
    .then(promiseToFrob)
    .catch(failedToFrob)

Foo, Bar, Frob. Fooing Barring Frobbing. , - Frob . , Frob, Frob Frobbing. - :

promiseToFoo()
    .then(promiseToBar)
    .catch(function (error) {
        failedToFooOrBar(error);
        return Promise.reject(error);
    })
    .then(function (x) {
        return promiseToFrob(x).catch(failedToFrob);
    });

, , on-reject catch , , . , . , . , , , on-, then, .

+1

All Articles