mirror of
https://github.com/denoland/deno.git
synced 2024-11-13 16:26:08 -05:00
feat(std/node): implement process.nextTick (#8386)
This commit is contained in:
parent
dd9c204884
commit
8ab20a4582
2 changed files with 41 additions and 0 deletions
|
@ -28,6 +28,25 @@ export const versions = {
|
||||||
...Deno.version,
|
...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 */
|
/** https://nodejs.org/api/process.html#process_process */
|
||||||
// @deprecated `import { process } from 'process'` for backwards compatibility with old deno versions
|
// @deprecated `import { process } from 'process'` for backwards compatibility with old deno versions
|
||||||
export const process = {
|
export const process = {
|
||||||
|
@ -123,6 +142,7 @@ export const process = {
|
||||||
// Getter also allows the export Proxy instance to function as intended
|
// Getter also allows the export Proxy instance to function as intended
|
||||||
return Deno.env.toObject();
|
return Deno.env.toObject();
|
||||||
},
|
},
|
||||||
|
nextTick,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
|
||||||
import * as path from "../path/mod.ts";
|
import * as path from "../path/mod.ts";
|
||||||
import * as all from "./process.ts";
|
import * as all from "./process.ts";
|
||||||
import { argv, env } 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
|
// 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)
|
// (Also Deno.env.toObject() (and process.env) requires --allow-env but it's more obvious)
|
||||||
|
@ -164,3 +165,23 @@ Deno.test({
|
||||||
// assert(process.stderr.isTTY);
|
// 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);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue