Skip to content

Commit

Permalink
Merge pull request #27 from syumai/resolve-wait-until-promise-explicitly
Browse files Browse the repository at this point in the history
resolve wait until promise explicitly
  • Loading branch information
syumai committed Mar 29, 2023
2 parents 49bfbf6 + 3ddba29 commit 3dcf908
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
- [x] Calling stubs
* [x] D1 (alpha)
* [x] Environment variables
* FetchEvent's [lifecycle methods](https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#lifecycle-methods)
- [x] waitUntil
- [ ] respondWith
- [ ] passThroughOnException

## Installation

Expand Down
18 changes: 0 additions & 18 deletions cloudflare/context.go

This file was deleted.

25 changes: 25 additions & 0 deletions cloudflare/fetchevent.go
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()
})))
}

0 comments on commit 3dcf908

Please sign in to comment.