How to rerun failed specs after cypress run

Note: this question is not related to test retries, this is about running the whole spec again from beginning, not about retrying a failed it() block within a spec.

According to Cypress docs:
you can rerun failing spec/specs after the catching the failures of process. I cannot find a way to do so. My current code is as below. Please add necessary steps to rerun failed specs in the same build then eventually fail/exit after a certain amount of failed rerun attempts

await cypress
    .then(result => {
      let exitCode = 0
      if (result.runs)
        if (result.runs.length > 0) {
          for (let i = 0; i < result.runs.length; i++) {
            for (let j = 0; j < result.runs[i].tests.length; j++) {
              if (result.runs[i].tests[j].state === 'failed') {
                // any logic here to rerun the failed specs?
                // may be a recursion fuction to call again with failed specs in the runOptions?
      process.exitCode = exitCode
    .catch(err => {

Source: Ask Javascript Questions