The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

569532 (7) [Avatar] Offline
#1
What is the best way to handle errors using Yield.
If we're using a promise, we can handle the error, but using yield is a different story. I liked using yield so far as it keeps the code readable. So what I'm doing to handle errors is the following:

try {
        // Get queue name
        let urlRequest = yield sqs.getQueueUrl({ QueueName: webhooks_sqs }).promise();

        if (_.get(urlRequest, "QueueUrl", false) === false)
            throw new Error(`error retreiving queue URL: [${webhooks_sqs}]`);
        ......
} catch (e) {
         console.log(e.message);
}



Do you think this is the best way to do that?
Morgatz (10) [Avatar] Offline
#2
This is pretty much how you do it with async/await which I'd recommend using it over co, generator functions, and yield

async yourFunctionName() {
  try {
    // Get queue name
    let urlRequest = await sqs.getQueueUrl({ QueueName: webhooks_sqs }).promise();
    if (_.get(urlRequest, "QueueUrl", false) === false)
        throw new Error(`error retreiving queue URL: [${webhooks_sqs}]`);
    ......
  } catch (e) {
    console.log(e.message)
  }
}