Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using JavaScript Promise All – is this syntax valid?

I have two tables with users, where each id for one user is same in both tables (don’t ask why I have two user tables).
At some point, I need to filter users from table 1, and if certain condition is true, I store a promise (deleting request) for each user into (let’s call it) tableOnePromises. I do the same for table 2.
In order to empty table 2, I MUST first empty table one due to some requirements.
this is what I did:

let tableOnePromises = [];
let tableTwoPromises = [];

tableOne.forEach(item => {
  if(item.deactivated) {
    const tableOneDeleted = supabase
      .from("table-one")
      .delete()
      .match({id: item.id});

    tableOnePromises.push(tableOneDeleted);

    const tableTwoDeleted = supabase
      .from("table-two")
      .delete()
      .match({id: item.id});

    tableOnePromises.push(tableTwoDeleted);
  }
});

await Promise.all(tableOnePromises).then(() => {
  return Promise.all(tableTwoPromises)
}).catch(err => console.log(err));

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Assuming the code using await is inside an async function (or at the top level of a module), the syntax is correct, but it’s probably not what I’d use. Mixing async/await with explicit callback hookup like .then and .catch is confusing and unnecessary. Instead, just use try/catch and a second await:

try {
    await Promise.all(tableOnePromises);
    await Promise.all(tableTwoPromises);
} catch (err) {
    console.log(err);
}

That said, I would only do that error catching if this is the top-level entry into your code, not if it’s called by another function in your code. If it’s called by another function in your code, I’d allow the error to propagate to the caller to be handled there.

(I’d also probably use for-of rather than .forEach, but it’s a style choice.)


…I MUST first empty table one due to some requirements…

If by "empty" you mean just that you have to ensure you’ve done the delete for a particular id on table one before doing it on table two, I’d change the code a bit more:

async function deleteItem(id) {
    await supabase
        .from("table-one")
        .delete()
        .match({id});
    await supabase
        .from("table-two")
        .delete()
        .match({id});
}

const promises = [];
for (const {deactivated, id} of tableOne) {
    if (deactivated) {
        promises.push(deleteItem(id));
    }
}
await Promise.all(promises); // With the `try`/`catch` if desired

Or if it’s okay to make two passes through the array:

async function deleteItem(id) {
    await supabase
        .from("table-one")
        .delete()
        .match({id});
    await supabase
        .from("table-two")
        .delete()
        .match({id});
}

await Promise.all( // With the `try`/`catch` if desired
    tableOne.filter(({deactivated}) => deactivated)
            .map(({id}) => deleteItem(id))
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading