Can I use .then() with query.trigger()

We have a javascript query that triggers a bunch of other queries in succession and using the .onSuccess() callback leads to some pretty gnarly spaghetti code. I saw the documentation on query.trigger() and promises and tried to use .then() to link my queries:

updateLocation.trigger(queryScope).then(createDistribution.trigger({additionalScope:{...queryScope.additionalScope,distributionTypeId:distributionTypeId()}}))

but that doesn't seem to do what I want–both queries are still getting called.

Am I doing something wrong?

OK! I'm still trying to find a clean, non-spaghetti way to call a bunch of queries in succession.

I built a scratch retool to test things out. First I created two simple queries that take a few seconds to run:

Then, I wrote a JS query to trigger them in succession.

const asyncifyQuery = (query,scope) => {
  return new Promise((resolve,reject) => {
    query.trigger({
      additionalScope:scope,
      onSuccess:(d) => resolve(d),
      onFailure: (d) => reject(d)
    });
  })
}

asyncifyQuery(five,null).then(asyncifyQuery(seven,null))

But even that is triggering both at the same time.

image

Does anyone have any thoughts?

Hey Alex,
I've set up two functions similar to your five & seven. Then I have a js function that calls them both, sequentially. Both the commented-out code, and the (slightly nicer, imo) await code work. Will either of these work for you?

5 Likes

Yup! As I discussed with @ben in intercom, the problem with the above code was a missing anonymous function inside the then.

So this code here works…

five.trigger().then(() => seven.trigger())

1 Like