mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
Merge pull request #27 from syumai/resolve-wait-until-promise-explicitly
resolve wait until promise explicitly
This commit is contained in:
commit
3dcf908475
@ -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
|
||||
|
||||
|
@ -1,18 +0,0 @@
|
||||
package cloudflare
|
||||
|
||||
import (
|
||||
"context"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/syumai/workers/cloudflare/internal/cfruntimecontext"
|
||||
"github.com/syumai/workers/internal/jsutil"
|
||||
)
|
||||
|
||||
func WaitUntil(ctx context.Context, task func()) {
|
||||
executionContext := cfruntimecontext.GetExecutionContext(ctx)
|
||||
|
||||
executionContext.Call("waitUntil", jsutil.NewPromise(js.FuncOf(func(this js.Value, args []js.Value) any {
|
||||
task()
|
||||
return nil
|
||||
})))
|
||||
}
|
25
cloudflare/fetchevent.go
Normal file
25
cloudflare/fetchevent.go
Normal file
@ -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()
|
||||
})))
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user