CodeSampleX

示例

es-toolkit 1.50.0: delay

已验证示例 — npm es-toolkit 1.50.0: delay. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: delay returns a Promise that resolves with undefined after the…

sha256:c40cef11f1a785d6172fd7c88e338c3320c224264a00636368979db21fe6d0a6

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 node linux 24 · ubuntu · glibc 2.39 x64 node node npm

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-27

案例

HOW
目标
verify es-toolkit.delay in pkg:npm/es-toolkit@1.50.0
包
符号
  • es-toolkit.delay
环境
node
创建时间
2026-08-27T23:53:58Z

契约

  1. delay returns a Promise that resolves with undefined after the given duration
  2. delay is exported identically from the package root and es-toolkit/promise subpath
  3. delay cancels the pending timer and rejects with an AbortError when the provided AbortSignal is aborted during execution
  4. delay rejects immediately with an AbortError when the AbortSignal is already aborted prior to invocation
  5. compat delay invokes the callback function after the wait duration with supplied arguments and returns a timer id
  6. compat delay throws a TypeError when the first argument is not a function

文件

  • NOTES.md
  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

下载源代码构件 (tar.gz)

源代码

NOTES.md
# es-toolkit delay API Behavior and Verification

### What search_known_solution answered
- `search_known_solution` returned `NO_SAFE_MATCH` for verifying `es-toolkit.delay` in `pkg:npm/es-toolkit@1.50.0`.
- This sample proves the runtime contract and cancellation semantics for `delay` in `es-toolkit@1.50.0`.

### Release
- Package: `es-toolkit`
- Version: `1.50.0` (pinned in `package.json` and `package-lock.json`)

### Verified Behaviors

1. **Modern Promise-Based Delay (`es-toolkit`, `es-toolkit/promise`)**:
   - `delay(ms)` returns a `Promise<void>` that resolves with `undefined` after `ms` milliseconds.
   - It can be imported from the package root `es-toolkit` or from `es-toolkit/promise`.

2. **AbortSignal Cancellation Support**:
   - `delay(ms, { signal })` accepts an optional `AbortSignal`.
   - If `controller.abort()` is called while the delay is in progress, the active timer is cleared and the promise rejects with an `AbortError` (subclass of `DOMException`).
   - If `signal` is already aborted prior to calling `delay`, the promise immediately rejects with an `AbortError` without starting a timer.

3. **Lodash Compatibility Delay (`es-toolkit/compat`)**:
   - `es-toolkit/compat` exports a Lodash-compatible `delay(func, wait, ...args)` that accepts a callback function and delay duration in milliseconds, returning a timer ID.
   - Throws `TypeError: Expected a function` if the first argument is not a callable function.
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify es-toolkit.delay in pkg:npm/es-toolkit@1.50.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/es-toolkit@1.50.0
Demonstrate these symbols/APIs:
  - es-toolkit.delay

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:73553476f69b0436a1ca523ad6c3640765ac1f5b83ed3b6f7f3ae1011c662a98","contract":["delay returns a Promise that resolves with undefined after the given duration","delay is exported identically from the package root and es-toolkit/promise subpath","delay cancels the pending timer and rejects with an AbortError when the provided AbortSignal is aborted during execution","delay rejects immediately with an AbortError when the AbortSignal is already aborted prior to invocation","compat delay invokes the callback function after the wait duration with supplied arguments and returns a timer id","compat delay throws a TypeError when the first argument is not a function"],"goal":"verify es-toolkit.delay in pkg:npm/es-toolkit@1.50.0","kind":"HOW","packages":["pkg:npm/es-toolkit@1.50.0"],"schemaVersion":1,"symbols":["es-toolkit.delay"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"node","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/es-toolkit@1.50.0"],"schemaVersion":1,"subject":"pkg:npm/es-toolkit@1.50.0","symbols":["es-toolkit.delay"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "npm-es-toolkit-v1",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "npm-es-toolkit-v1",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "es-toolkit": "1.50.0"
      }
    },
    "node_modules/es-toolkit": {
      "version": "1.50.0",
      "resolved": "https://registry.npmjs.org/es-toolkit/-/es-toolkit-1.50.0.tgz",
      "integrity": "sha512-OyZKhUVvEep9ITEiwHn8GKnMRQIVqoSIX7WnRbkWgJkllCujilqP2rD0u979tkl8wqyc8ICwlc1UBVv/Sl1G6w==",
      "license": "MIT",
      "workspaces": [
        "docs",
        "benchmarks",
        "tests/types"
      ]
    }
  }
}
package.json
{
  "name": "npm-es-toolkit-v1",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "es-toolkit": "1.50.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify es-toolkit.delay in pkg:npm/es-toolkit@1.50.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/es-toolkit@1.50.0"
  ],
  "symbols": [
    "es-toolkit.delay"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { delay, AbortError } from 'es-toolkit';
import { delay as promiseDelay } from 'es-toolkit/promise';
import { delay as compatDelay } from 'es-toolkit/compat';

// 1. delay resolves after elapsed time
{
  const start = Date.now();
  const res = await delay(20);
  const elapsed = Date.now() - start;
  assert.strictEqual(res, undefined);
  assert.ok(elapsed >= 15, `Expected elapsed time >= 15ms, got ${elapsed}ms`);
}

// 2. delay from es-toolkit/promise is identical to standard export
{
  const res = await promiseDelay(10);
  assert.strictEqual(res, undefined);
}

// 3. delay is cancellable via AbortSignal during execution
{
  const controller = new AbortController();
  const timeoutId = setTimeout(() => controller.abort(), 10);
  
  await assert.rejects(
    async () => {
      await delay(500, { signal: controller.signal });
    },
    (err) => {
      assert.ok(err instanceof AbortError);
      assert.ok(err instanceof DOMException);
      assert.strictEqual(err.message, 'The operation was aborted');
      return true;
    }
  );
  clearTimeout(timeoutId);
}

// 4. delay rejects immediately if AbortSignal is already aborted
{
  const controller = new AbortController();
  controller.abort();

  await assert.rejects(
    async () => {
      await delay(100, { signal: controller.signal });
    },
    (err) => {
      assert.ok(err instanceof AbortError);
      assert.ok(err instanceof DOMException);
      return true;
    }
  );
}

// 5. es-toolkit/compat delay invokes callback after wait with arguments and returns timer id
{
  let called = false;
  let receivedArgs = [];
  
  const timerId = compatDelay(
    (a, b) => {
      called = true;
      receivedArgs = [a, b];
    },
    20,
    'foo',
    'bar'
  );

  assert.ok(timerId !== undefined);
  await delay(40);
  assert.strictEqual(called, true);
  assert.deepStrictEqual(receivedArgs, ['foo', 'bar']);
}

// 6. es-toolkit/compat delay throws TypeError if first argument is not a function
{
  assert.throws(
    () => compatDelay('not a function', 10),
    {
      name: 'TypeError',
      message: 'Expected a function',
    }
  );
}

原始种子者

匿名