How to return from a Promise's catch/then block?
Date : March 29 2020, 07:55 AM
may help you . This can't be achieved with features of the language. However, pattern-based solutions are available. Here are two solutions. Promise.resolve()
.then(Function1).catch(errorHandler1)
.then(Function2).catch(errorHandler2)
.then(Function3).catch(errorHandler3)
.then(Function4).catch(errorHandler4)
.catch(finalErrorHandler);
function errorHandler1(error) {
if (error instanceof MyCustomError) { // <<<<<<< test for previously thrown error
throw error;
} else {
// do errorHandler1 stuff then
// return a result or
// throw new MyCustomError() or
// throw new Error(), new RangeError() etc. or some other type of custom error.
}
}
Promise.resolve()
.then(function() { return Function1().catch(errorHandler1); })
.then(function() { return Function2().catch(errorHandler2); })
.then(function() { return Function3().catch(errorHandler3); })
.then(function() { return Function4().catch(errorHandler4); })
.catch(finalErrorHandler);
|
Promise: Ignore Catch and Return to Chain
Date : March 29 2020, 07:55 AM
seems to work fine Is it possible to ignore a catch and return back to the chain? , Here's the synchronous analogy: try {
action1(); // throws
action2(); // skipped
action3(); // skipped
} catch (e) {
// can't resume
}
try {
action1(); // throws
} catch (e) {
handleError(e);
}
action2(); // executes normally
action3();
asyncActionA() // <-- fails with 'missing' reason
.catch(error => {
if(error.type == 'missing'){
return; // Makes sure the promise is resolved, so the chain continues
}
throw error; // Otherwise, rethrow to keep the Promise rejected
})
.asyncActionB(promiseB) // <-- runs
.asyncActionC(promiseC)
.catch(err => {
// Handle errors which are not of type 'missing'.
});
|
Promise catch() order in complex promise return chain
Date : March 29 2020, 07:55 AM
To fix this issue When using promises, a function should do one of three things: Return a value Return a promise Throw an error function post() {
foo.bar; // will throw
}
function run() {
post()
.then(log)
.catch(log);
}
ReferenceError: foo is not defined
at post (sync.js:6:3)
at run (sync.js:10:3)
at Object.<anonymous> (sync.js:15:1)
function post() {
return new Promise((resolve, reject) => {
reject('foo');
});
}
function run() {
post()
.then(() => {})
.catch((err) => {
console.error('Caught error:', err);
});
}
function post() {
return new Promise((resolve, reject) => {
resolve('foo');
});
}
function run() {
post()
.then((result) => {
throw result;
})
.catch((err) => {
console.error('Caught error:', err);
});
}
|
Throwing inside a catch block in a nested Promise to trigger the catch block of outer Promise, is there an alternative c
Date : March 29 2020, 07:55 AM
Any of those help I think the clean way to do this would be using async/await. But before going there, is your question how to not run the outer promise when the inner promise fails? The example below: const fun42 = () => {
return new Promise((resolve, reject) => {
setTimeout(() =>{
resolve(42)
reject('something at fun 42 went wrong')
}, 500)
})
}
const fun32 = () => {
return new Promise((resolve, reject) => {
setTimeout(() =>{
//resolve(32)
reject('something at fun 32 went wrong')
}, 500)
})
}
fun32().then(x => {
console.log(x)
return fun42()
}).then( y => {
console.log(y)
}).catch (e => {
console.log(e)
})
|
Firebase Promise chain terminate in catch block
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Something like this should work, using a combination a propagated values and a top-level sentinel: let bail = false
doWork()
.then(result => {
console.log(result)
return true // indicate success
})
.catch(error => {
console.error(error)
return false // indicate error
})
.then(isPriorSuccessful => {
if (!isPriorSuccessful) {
bail = true
return null
}
else {
// do more stuff here, return a promise
return doMoreWork()
}
})
.catch(error => {
console.error(error)
})
.then(() => {
if (bail) {
res.status(500).send("NOT OK")
return
}
console.log("Just before the end")
res.send("OK")
})
|