Skip to content

New "Wait For" Methods #20

@parker-codes

Description

@parker-codes

I think the new mutation status API could be improved with some functions that wait for completion instead of returning a PendingRequest instance and then having to manually loop until the request is finished.

Here is how it works currently:

const request = await todo.update({
  Completed: !completed,
});

// potentially wrap this in a loop
const requestCompleted = await request.isCompleted();

We can prefix mutation methods with "waitFor", like waitForDelete, that take the normal parameters, plus an optional object at the end that defines interval to recheck (ms) plus the number of tries.

This would be achieved by having a setInterval next to a setTimeout (with a calculated time based on interval * tries). Whichever comes first completes the Promise. If the timeout is reached, reject.

The new API usage could look like:

// basic
await todo.waitForUpdate({
  Completed: !completed,
}).catch(() => {
  console.log('Update did not finish in a reasonable timeframe');
});

// with control over timing
// this would check every 2 seconds up to 30 times, so up to a minute
// I doubt it would take anything close to a minute, but hey, this is just an example 😅
await todo.waitForUpdate({
  Completed: !completed,
}, {
  interval: 2000,
  tries: 30,
}).catch(() => {
  console.log('Update did not finish within a minute');
});

I think some reasonable defaults would be:
interval: 1000
tries:      10


TODOs

  • reach out to Coda team to get idea of average mutation completion time (I'll handle this)
  • decide on API
  • implement
  • test
  • document

@mjoyce @svennu @inguelberth @phlexib @fenixsolorzano
Sorry about the pings every so often, but I want to make sure this project follows the needs and use cases of users and maintainers. If you have a preference on this topic, speak up!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions