aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-12-02 01:56:12 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-12-02 01:56:12 -0800
commit8eddfc61a83657c770833665a0fb9926aa326603 (patch)
treeccb130aeb8664e0fa1800750eb7e66a11179399b /src/bun.js
parent37525db5cf2b8b609bd16cc9cfa0c80aa80f9f00 (diff)
downloadbun-8eddfc61a83657c770833665a0fb9926aa326603.tar.gz
bun-8eddfc61a83657c770833665a0fb9926aa326603.tar.zst
bun-8eddfc61a83657c770833665a0fb9926aa326603.zip
Add generic way to block on a promise
Diffstat (limited to '')
-rw-r--r--src/bun.js/base.zig22
-rw-r--r--src/bun.js/event_loop.zig13
-rw-r--r--src/bun.js/javascript.zig2
3 files changed, 19 insertions, 18 deletions
diff --git a/src/bun.js/base.zig b/src/bun.js/base.zig
index 6ca9e1084..f331dc8f5 100644
--- a/src/bun.js/base.zig
+++ b/src/bun.js/base.zig
@@ -2763,23 +2763,13 @@ pub fn wrapWithHasContainer(
if (comptime maybe_async) {
if (result.asPromise() != null or result.asInternalPromise() != null) {
var vm = ctx.ptr().bunVM();
- vm.tick();
- var promise = JSC.JSInternalPromise.resolvedPromise(ctx.ptr(), result);
- switch (promise.status(ctx.ptr().vm())) {
- JSC.JSPromise.Status.Pending => {
- while (promise.status(ctx.ptr().vm()) == .Pending) {
- vm.tick();
- }
- result = promise.result(ctx.ptr().vm());
- },
- JSC.JSPromise.Status.Rejected => {
- result = promise.result(ctx.ptr().vm());
- exception.* = result.asObjectRef();
- },
- JSC.JSPromise.Status.Fulfilled => {
- result = promise.result(ctx.ptr().vm());
- },
+ if (result.asPromise()) |promise| {
+ vm.waitForPromise(promise);
+ result = promise.result(ctx.vm());
+ } else if (result.asInternalPromise()) |promise| {
+ vm.waitForPromise(promise);
+ result = promise.result(ctx.vm());
}
}
}
diff --git a/src/bun.js/event_loop.zig b/src/bun.js/event_loop.zig
index 7fe248dad..0e2d16486 100644
--- a/src/bun.js/event_loop.zig
+++ b/src/bun.js/event_loop.zig
@@ -521,7 +521,18 @@ pub const EventLoop = struct {
}
// TODO: fix this technical debt
- pub fn waitForPromise(this: *EventLoop, promise: *JSC.JSInternalPromise) void {
+ pub fn waitForPromise(this: *EventLoop, promise: anytype) void {
+ return waitForPromiseWithType(this, std.meta.Child(@TypeOf(promise)), promise);
+ }
+
+ pub fn waitForPromiseWithType(this: *EventLoop, comptime Promise: type, promise: *Promise) void {
+ comptime {
+ switch (Promise) {
+ JSC.JSPromise, JSC.JSInternalPromise => {},
+ else => @compileError("Promise must be a JSPromise or JSInternalPromise, received: " ++ @typeName(Promise)),
+ }
+ }
+
switch (promise.status(this.global.vm())) {
JSC.JSPromise.Status.Pending => {
while (promise.status(this.global.vm()) == .Pending) {
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index 312c052f1..86b026ecd 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -560,7 +560,7 @@ pub const VirtualMachine = struct {
this.eventLoop().tick();
}
- pub fn waitForPromise(this: *VirtualMachine, promise: *JSC.JSInternalPromise) void {
+ pub fn waitForPromise(this: *VirtualMachine, promise: anytype) void {
this.eventLoop().waitForPromise(promise);
}