Make Do function return an array of evaluations of each expressions instead of the last expression only

If you run:

Do(
  Create(
    Collection('col1'),
    { data }
  ),
  Create(
    Collection('col2'),
    { data }
  ),
  Create(
    Collection('col3'),
    { data }
  )
)

You only get the evaluation of the last expression, it would be good if you get an array containing the evaluation of each expression instead.

Workaround to do this is

const [col1, col2, col3] = await Promise.all([
  fauna.query(
    Create(
      Collection('col1'),
      { data }
    )
  ),
  fauna.query(
    Create(
      Collection('col2'),
      { data }
    ),
  ),
  fauna.query(
    Create(
      Collection('col3'),
      { data }
    )
  )
]);

but this does 3 network requests…

An array is a valid expression to pass to a query. So you can do the following:

client.query([
  Create(
    Collection('col1'),
    { data }
  ),
  Create(
    Collection('col2'),
    { data }
  ),
  Create(
    Collection('col3'),
    { data }
  )
])
1 Like

@ptpaterson has given you the solution, but for posterity: the whole raison d’être for Do is to not keep the results of each evaluation. It’s equivalent to a semi-colon in many languages. It is a sequencing operation, chaining statements together. In pseudo fql Do(expr1, expr2, expr3) would be most like

expr1;
expr2;
expr3

If you want use the results of expressions for further computation then Let is the ticket. Let({x: expr_x, y: expr_y}, expr) could be more natually written as

let x = expr_x
    y = expr_y
 in expr

And lastly of course if you want to just evaluate an array of expressions and see all the results with no interaction between the separate expressions you can just pass an array of expressions to the client.

1 Like