Search code examples
javascriptpromisees6-promise

How to stop processing continuing past a promise reject


I have async code that I'd like to stop running when I reject.

Note:

let ingestPromises = event.Records.map(async (record, i) => {
    let camera = JSON.parse(record.body)
    return new Promise(async (resolve, reject) => {

        let imageBuffer;
        try {
            imageBuffer = await retrieveImageFromURI(camera.ing_uri)
            console.log(`Record #${i} Successful Ingestion from URI`);
        } catch (err) {
            console.log("Going to reject");
            reject({ 'Task': `Attempting to pull image from camera (${camera.camName}`})
            console.log("Rejected");
        }

        console.log("Going to save file now...");
   //    ... blah blah more code ...
})}

let settledPromises = await Promise.allSettled(ingestPromises)
console.log('All promises settled :>> ', settledPromises);

When I run this against an invalid uri, the retrieveImageFromURI function throws a timeout error as expected. This skips the "Record X successful ingestion..." message and jumps to the catch block as expected. However, then things go unexpectedly.

Output from the above code, when retrieveImageFromURI throws an error looks like this:

Going to reject
Rejected
Going to save file now...
All promises settled :>>  [ {
    status: 'rejected',
    reason: {
      Task: 'Attempting to pull image from camera (TEST Camera)'
    }
}]

It is my expectation that the reject would stop any further processing within that promise (much like a return would) and I would NOT see 'Rejected' nor 'Going to save file now...' console.logs...

What am I misunderstanding here?


Solution

  • There's nothing special about invoking reject. It's just a function. If you want to bail on further processing, your function should return.

    try {
      imageBuffer = await retrieveImageFromURI(camera.ing_uri)
      console.log(`Record #${i} Successful Ingestion from URI`);
    } catch (err) {
      console.log("Going to reject");
      reject({ 'Task': `Attempting to pull image from camera (${camera.camName}`})
      console.log("Rejected");
      return;
    }