Skip to content

feat: add waitFor utility to wait for a duration - #302

Open
gomesalexandre wants to merge 1 commit into
MetaMask:mainfrom
gomesalexandre:feat_waitfor_utility
Open

feat: add waitFor utility to wait for a duration#302
gomesalexandre wants to merge 1 commit into
MetaMask:mainfrom
gomesalexandre:feat_waitfor_utility

Conversation

@gomesalexandre

@gomesalexandregomesalexandre commented Aug 18, 2026

Copy link
Copy Markdown

closes#160

what

Adds waitFor(milliseconds), a promisified setTimeout for pausing execution inside an async function. As the issue notes, this helper is reimplemented across many MetaMask projects (test-dapp, metamask-sdk, metamask-extension, metamask-desktop), so consolidating it here removes the duplication.

import{waitFor,inMilliseconds,Duration}from'@metamask/utils';awaitwaitFor(500);awaitwaitFor(inMilliseconds(2,Duration.Second));

how

  • lives in src/time.ts alongside Duration / inMilliseconds, since it waits for a duration
  • validates its argument with the module's existing assertIsNonNegativeInteger, consistent with inMilliseconds and timeSince
  • kept the name from the issue (waitFor); happy to rename to wait/delay if you'd prefer

tests

Added unit tests covering the resolve-after-duration path (via jest fake timers), the zero-duration case, and rejection on a negative/non-integer input. time.ts is at 100% coverage; updated the index/node export snapshots. Full suite (1988 tests), tsc/build, tsd, eslint and prettier all pass.


Note

Cursor Bugbot is generating a summary for commit 2c0f4ab. Configure here.

Adds `waitFor(milliseconds)`, a promisified `setTimeout` for pausing
execution in an async function. This helper is duplicated across many
MetaMask projects (test-dapp, metamask-sdk, metamask-extension, ...), so
it makes sense to consolidate it here.
It lives in `time.ts` next to `Duration`/`inMilliseconds`, validates its
input with the module's existing `assertIsNonNegativeInteger`, and
composes with `inMilliseconds` for other durations, e.g.
`waitFor(inMilliseconds(2, Duration.Second))`.
closesMetaMask#160
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add utility to wait for a designated amount of time

1 participant

@gomesalexandre