mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-04 01:40:08 +00:00
I prefer semicolors, they also help avoiding certain pitfalls in JavaScript/TypeScript, such as the following code sample: ```js const xyz = "test" (something.else as string) = "another" ``` This results in a TypeError: "test" is not a function, this is because js thinks we are trying to call the string "test" as a function. To fix this it requires a `;` somewhere before the `(`, such as `;(something ... ` which in my opinion is ugly and less clean overall.
26 lines
951 B
TypeScript
26 lines
951 B
TypeScript
import { dirname } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
// This is a "polyfill" for the `Promise.withResolves`, while node 22 does support it, node 18 does not and this template does support node 18
|
|
export function promiseWithResolvers<T>(): PromiseWithResolvers<T> {
|
|
let resolve!: (data: T | PromiseLike<T>) => void;
|
|
let reject!: (reason?: any) => void;
|
|
const promise = new Promise<T>((_resolve, _reject) => {
|
|
resolve = _resolve;
|
|
reject = _reject;
|
|
});
|
|
|
|
return { promise, resolve, reject };
|
|
}
|
|
|
|
// This re-creates the `__dirname` that exists in CommonJS, in ESM node added `import.meta.dirname` but it is for node 20+, so we need this util to support node 18
|
|
export function getDirnameFromFileUrl(url: string): string {
|
|
return dirname(fileURLToPath(url));
|
|
}
|
|
|
|
export interface PromiseWithResolvers<T> {
|
|
promise: Promise<T>;
|
|
resolve: (data: T | PromiseLike<T>) => void;
|
|
reject: (reason?: any) => void;
|
|
}
|