Advanced

Non-Determinism

Your script may be executed multiple times in order to support the undo, or when resuming execution after the user refreshes the page. Multiple executions can't follow different code paths without causing inconsistencies, so we must enforce some measure of non-determinism.

When a respondent first loads the form, we:

  • Generate a random seed for Math.random
  • Lock the Date in place

And from then on until the form has been submitted (or you Start Over):

  • new Date() and Date.now() will always return the same value
  • Successive calls to Math.random() will return the same sequence of values

In addition, responses to form.fetch() calls are cached until the form has been submitted.

Example

You can try this yourself. Take the following form definition:

export default async function () {
  let randomValue = Math.random();
  let date = Date.now();

  await form.short("First question");

  form.log("randomValue", randomValue);
  form.log("date", date);

  await form.short("Second question");
}

Which produces this form:

Answer the first question but not the second. Look in the browser console, and you should see something like this:

Now refresh this page, which restarts execution of the embedded form. The form should resume at the second question, and you should see the same random value and date in the browser console.