Skip to content

try/catch doesn't work #309

Description

@wzup

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions