co
co copied to clipboard
try/catch doesn't work
Use case. The goal is to stop code execution that follows try/catch, if the try/catch has an error. But it executes.
let urls = ["url", "url", "url"]
co(function* () {
let promises = urls.map(url => {
return request_promise(url);
})
// so x is an array with results from request
let x = yield promises; // x[]
// suppose if JSON.parse(x) throws an error
let j;
try {
j = JSON.parse(x);
}
catch(e) {
// HOW DO I PASS THIS ERROR ON to .catch() block of this generator?
// the code below this try/catch doesn't have to be executed and .then block doesn't have to called
???
}
// I have code here too
// And I don't want it to execute if try/catch throws an error
// but it however executes
... do some stuff with x
return { some_result: foo };
})
.then(result => {
// this block shouldn't execute it try/catch has error, but it does. why?
res.send(200).json(result)
})
.catch(err => {
// the error from try/catch has to be passed on here
res.send(500).json({err: err})
})
Do I oversee something here? The normal JS behavior: If you "catch" an exception, it is resolved and normal execution goes on. Just add another "throw e" in your "catch" block or don't "try/catch" at all. Does this help?