event.waitUntil fetch with R2


I’m trying to run a fetch post and save to an R2 bucket AFTER returning a response…

// this saves to R2
await doThing()
return new Response()

// this fires the fetch but does not save to R2
return new Response()
const doThing = () =>
    new Promise((resolve) => {
        fetch('https://example.com', {
            method: 'POST',
            body: JSON.stringify({
                foo: bar
            .then((res) => res.json())
            .then(async (data) => {
                await R2_BUCKET.put('key', data)

Any ideas on what I’m doing wrong here?

How long does doThing() take? The waitUntil() function only delays eviction for 30 seconds.

1 Like

Ah yep I’ve done a few tests and it’s the fetch taking too long. Hmm I guess I’ll need to build a workaround.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.