OpenFn / OpenFn/adaptors

common: add defer function

Open
#640 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

DevX
Dominant language
JavaScript
Stars
24
Forks
41
Avg merge
3d 13h
Merged PRs (30d)
12

Description

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))
)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the existing common fn and each implementations and their tests. Add the proposed defer(durationInSeconds, fn) operation so it composes with job state and waits in seconds, then verify the delay and composition behavior with the project's existing tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.