gja / cloudflare-worker-local

Run a Cloudflare Worker Locally
Apache License 2.0
171 stars 37 forks source link

It seems setTimeout is not working #29

Closed sheerun closed 5 years ago

sheerun commented 5 years ago

The function I'm trying to use:

function sleep(ms) {
  return new Promise(resolve => {
    setTimeout(resolve, ms)
  })
}

I can see similar method working in tutorials like this: https://scotthelme.co.uk/unleashing-the-power-of-cloudflare-workers/

While testing with this package I get:

[0] ReferenceError: setTimeout is not defined
[0]     at resolve (evalmachine.<anonymous>:741:5)
[0]     at new Promise (<anonymous>)
gja commented 5 years ago

Hi. Could you elaborate a bit on your use case? I’ll add setTimeout, as I know that cloudflare does support it, but I’m curious, as I’m not sure cloudflare guarantees that it will wait for your callback to fire. IAC I’ll fix this in a day or two.

sheerun commented 5 years ago

@gja Cloudflare supports construct like await sleep(1000) for the function above the same way as you can await fetch() when fetching external resources from cloudflare workers scripts.

My use case is to wait 2 seconds before answering Nexmo webhook what would simulate 2s delay before picking up the call