Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try/catch doesn't work #309

Open
wzup opened this issue Dec 26, 2016 · 1 comment
Open

try/catch doesn't work #309

wzup opened this issue Dec 26, 2016 · 1 comment

Comments

@wzup
Copy link

wzup commented Dec 26, 2016

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})
})
@freiit
Copy link

freiit commented Dec 29, 2016

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants