Skip to content

Commit

Permalink
feat(std/node): implement process.nextTick (denoland#8386)
Browse files Browse the repository at this point in the history
  • Loading branch information
Soremwar authored and jannes committed Dec 1, 2020
1 parent ebfd0d2 commit d3f7ac7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions std/node/process.ts
Expand Up @@ -28,6 +28,25 @@ export const versions = {
...Deno.version,
};

/** https://nodejs.org/api/process.html#process_process_nexttick_callback_args */
export function nextTick(this: unknown, cb: () => void): void;
export function nextTick<T extends Array<unknown>>(
this: unknown,
cb: (...args: T) => void,
...args: T
): void;
export function nextTick<T extends Array<unknown>>(
this: unknown,
cb: (...args: T) => void,
...args: T
) {
if (args) {
queueMicrotask(() => cb.call(this, ...args));
} else {
queueMicrotask(cb);
}
}

/** https://nodejs.org/api/process.html#process_process */
// @deprecated `import { process } from 'process'` for backwards compatibility with old deno versions
export const process = {
Expand Down Expand Up @@ -123,6 +142,7 @@ export const process = {
// Getter also allows the export Proxy instance to function as intended
return Deno.env.toObject();
},
nextTick,
};

/**
Expand Down
21 changes: 21 additions & 0 deletions std/node/process_test.ts
Expand Up @@ -5,6 +5,7 @@ import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
import * as path from "../path/mod.ts";
import * as all from "./process.ts";
import { argv, env } from "./process.ts";
import { delay } from "../async/delay.ts";

// NOTE: Deno.execPath() (and thus process.argv) currently requires --allow-env
// (Also Deno.env.toObject() (and process.env) requires --allow-env but it's more obvious)
Expand Down Expand Up @@ -164,3 +165,23 @@ Deno.test({
// assert(process.stderr.isTTY);
},
});

Deno.test({
name: "process.nextTick",
async fn() {
let withoutArguments = false;
process.nextTick(() => {
withoutArguments = true;
});

const expected = 12;
let result;
process.nextTick((x: number) => {
result = x;
}, 12);

await delay(10);
assert(withoutArguments);
assertEquals(result, expected);
},
});

0 comments on commit d3f7ac7

Please sign in to comment.