diff options
author | 2023-07-26 23:52:57 -0700 | |
---|---|---|
committer | 2023-07-26 23:52:57 -0700 | |
commit | dc0d767f27ca9528f7589e79bf89a65000935b40 (patch) | |
tree | cbe572261ffb40d467dc9f67ce4ae206a9d0d0f6 | |
parent | 86633e0af47dc9ff62e9cef23962a855d061bde8 (diff) | |
download | bun-dc0d767f27ca9528f7589e79bf89a65000935b40.tar.gz bun-dc0d767f27ca9528f7589e79bf89a65000935b40.tar.zst bun-dc0d767f27ca9528f7589e79bf89a65000935b40.zip |
Enable `Promise.withResolvers()`
https://github.com/tc39/proposal-promise-with-resolvers
Thanks @dcrousso
-rw-r--r-- | packages/bun-types/globals.d.ts | 30 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.cpp | 2 |
2 files changed, 31 insertions, 1 deletions
diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts index 885294b0d..ed22be080 100644 --- a/packages/bun-types/globals.d.ts +++ b/packages/bun-types/globals.d.ts @@ -3565,7 +3565,7 @@ interface WorkerOptions { /** * Does nothing in Bun */ - type?: string + type?: string; } interface WorkerEventMap { @@ -3648,3 +3648,31 @@ declare var EventSource: { readonly CONNECTING: number; readonly OPEN: number; }; + +interface PromiseConstructor { + /** + * Create a deferred promise, with exposed `resolve` and `reject` methods which can be called + * separately. + * + * This is useful when you want to return a Promise and have code outside the Promise + * resolve or reject it. + * + * ## Example + * ```ts + * const { promise, resolve, reject } = Promise.withResolvers(); + * + * setTimeout(() => { + * resolve("Hello world!"); + * }, 1000); + * + * await promise; // "Hello world!" + * ``` + * + * `Promise.withResolvers()` is a [stage3 proposal](https://github.com/tc39/proposal-promise-with-resolvers). + */ + withResolvers<T>(): { + promise: Promise<T>; + resolve: (value?: T | PromiseLike<T>) => void; + reject: (reason?: any) => void; + }; +} diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index 946e368dd..746412d78 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -258,6 +258,8 @@ extern "C" void JSCInitialize(const char* envp[], size_t envc, void (*onCrash)(c JSC::Options::useJITCage() = false; JSC::Options::useShadowRealm() = true; JSC::Options::useResizableArrayBuffer() = true; + JSC::Options::usePromiseWithResolversMethod() = true; + #ifdef BUN_DEBUG JSC::Options::showPrivateScriptsInStackTraces() = true; #endif |