Overview

Defer allows you to automatically cancel an execution if it hasn’t been processed within a set time frame. Executions set to be discarded must start before discard time and can run as long they want. This is useful, for example, if you want to avoid sending expired 2FA codes to your customers.

You can configure the discard option of a function with a specific datetime or a duration.

const withDiscard = assignOptions(myFunc, { discardAfter: "1h" });

There could be a conflict between the discard and delay options. For instance, if you choose to discard an execution after 5 minutes but have also delayed it for 1 hour, the execution will be discarded. To avoid this conflict, you should set the discard option for 1 hour and 5 minutes.

Example

import { assignOptions } from "@defer/client";
import send2FACode from "./defer/send2FACode";

const discardableSend2FACode = assignOptions(send2FACode, {
  discardAfter: "5m",
});
await discardableSend2FACode(userId);