Help with control flow in multi-prompt scripts

I’m trying to create a script to create new records in Airtable, fill out various fields, using the Drafts Prompts. I’m using the wonderful Airtable API for drafts to make communicating with Airtable easier, but I need some help with controlling the flow/error handling.

I understand there is no way to hard-exit from a script, and it seems tedious/difficult to separate the script into smaller script action steps to be able to use context.cancel for this. Do I have to wrap the script in nested if statements (one for each prompt)? That doesn’t seem very clean or readable…

I’m trying to avoid this:

let con = p.show();
if (con) {
  let con = p.show();
  if (con) {
    let con = p.show();
    if (con) {
      etc.
    }
  }
};

Is this a better way to do it:

let con = p.show();
let con2 = false;
let con3 = false;
let con4 = false;

if (con) {
  con2 = p.show()
}
if (con2) {
  con3 = p.show()
}
etc.

It’s probably quite obvious that I’m no programmer by now, but is there a better way to handle this? I want to be able to stop the script whenever a prompt is cancelled, to avoid going through the rest of the prompts.

You can use a wrapper function. I generally do something like this:

// create a wrapper function that returns a boolean
let f = () => {
    let p = new Prompt();
    // configure prompt
    if (!p.show()) {
        return false;
    }

    let p2 = new Prompt();
    // configure prompt
    if (!p2.show()) {
        return false;
    }

    // we have all the info, do the work!
    return true;
}

// call the wrapper, use result to control cancellation
if (!f()) {
    context.cancel();
}

2 Likes

Thank you! A much more elegant solution. It does just what I need.

1 Like