-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from syumai/resolve-wait-until-promise-explicitly
resolve wait until promise explicitly
- Loading branch information
Showing
3 changed files
with
29 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"syscall/js" | ||
|
||
"github.com/syumai/workers/cloudflare/internal/cfruntimecontext" | ||
"github.com/syumai/workers/internal/jsutil" | ||
) | ||
|
||
// WaitUntil extends the lifetime of the "fetch" event. | ||
// It accepts an asynchronous task which the Workers runtime will execute before the handler terminates but without blocking the response. | ||
// see: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#waituntil | ||
func WaitUntil(ctx context.Context, task func()) { | ||
executionContext := cfruntimecontext.GetExecutionContext(ctx) | ||
|
||
executionContext.Call("waitUntil", jsutil.NewPromise(js.FuncOf(func(this js.Value, pArgs []js.Value) any { | ||
resolve := pArgs[0] | ||
go func() { | ||
task() | ||
resolve.Invoke(js.Undefined()) | ||
}() | ||
return js.Undefined() | ||
}))) | ||
} |