Execute something after return

I want to use Workers AI on Discord through my bot, AI content takes a long time to generate, so I need to respond Discord first and update the result later. Is there a way to solve it? By the way, how do I get my account ID?
9 Replies
DaniFoldi
DaniFoldi6mo ago
Hi meowwave Let me answer your second question first, your account id can be found in multiple locations: go to workers & pages in the dashboard, and find it on the right hand side in a click-to-copy box. It is also in the url, a long hexadecimal string As for the first one, workers have a special way to achieve this: https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/#contextwaituntil In that waitUntil, put your promise that resolves after the initial response, so your code looks something like this:
context.waitUntil(aiPromise.then(someResponseModification).then(editMessage))
return new Response(...)
context.waitUntil(aiPromise.then(someResponseModification).then(editMessage))
return new Response(...)
AngusMa
AngusMa6mo ago
Thanks for your help, I copied example from Discord documentation, it doesn’t have the context argument. How can I use it?
router.post('/', async (request, env) => {
router.post('/', async (request, env) => {
DaniFoldi
DaniFoldi6mo ago
hmm, what router is that example using?
AngusMa
AngusMa6mo ago
GitHub
cloudflare-sample-app/src/server.js at main · discord/cloudflare-sa...
Example discord bot using Cloudflare Workers. Contribute to discord/cloudflare-sample-app development by creating an account on GitHub.
kian
kian6mo ago
Just add a third param, after env async (request, env, ctx) => { https://github.com/kwhitley/itty-router#4-only-one-required-argument--the-rest-is-up-to-you
DaniFoldi
DaniFoldi6mo ago
itty-router it is haha
AngusMa
AngusMa6mo ago
I tried but failed
TypeError: Cannot read properties of undefined (reading 'waitUntil')
at null.<anonymous> (file:///cloudflare-sample-app/src/server.js:161:13)
at async Object.handle (file:///cloudflare-sample-app/node_modules/itty-router/index.mjs:1:614)
at async jsonError (file:///cloudflare-sample-app/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:10)
TypeError: Cannot read properties of undefined (reading 'waitUntil')
at null.<anonymous> (file:///cloudflare-sample-app/src/server.js:161:13)
at async Object.handle (file:///cloudflare-sample-app/node_modules/itty-router/index.mjs:1:614)
at async jsonError (file:///cloudflare-sample-app/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:10)
kian
kian6mo ago
const server = {
verifyDiscordRequest: verifyDiscordRequest,
fetch: async function (request, env, env) {
return router.handle(request, env, env);
},
};
const server = {
verifyDiscordRequest: verifyDiscordRequest,
fetch: async function (request, env, env) {
return router.handle(request, env, env);
},
};
Modify the server object at the bottom as well to include them, like this
AngusMa
AngusMa6mo ago
It works! Thank you so much!