OpenFn / adaptors

The new home for OpenFn adaptors; re-usable connectors for the most common DPGs and DPI building blocks.
GNU General Public License v3.0
4 stars 8 forks source link

common: add defer function #640

Open josephjclark opened 2 weeks ago

josephjclark commented 2 weeks ago

defer is like fn, but it waits for n SECONDS before executing

defer(durationInSeconds, fn)

Where fn is any operation

Seconds because ms is unlikely to be useful, and you can always to 0.1s. Usually JS timeouts are in ms so this might catch people out and we need to be very clear.

The implementation is like:

const defer = (durationInSections, fn) => state => {
  return new Promise((resolve) => {
    setTimeout(() => {
      fn(state)
    }, 1000 * durationInSeconds)
  });
}

You can do this in job code today but it's a bit gnarly, and it feels like a useful pattern to support.

You can compose with each nicely:

each(
  '$.patients[*]',
  defer(5, post('/patients', $.data))
)