executionCtx.waitUntil on write service

I have a worker that I want to call an external api on, but I want to return a response without awaiting the the external api call. (the external api call is to just save time writing to the db on another service).
const promise = fetch('http://localhost:3000/impression', {
method: "POST",
body: JSON.stringify(data),
headers: {
"content-type": "application/json",
}
});
c.executionCtx.waitUntil(promise.then())

flow.params = { campaignId: campaignId }
return flow;

const promise = fetch('http://localhost:3000/impression', {
method: "POST",
body: JSON.stringify(data),
headers: {
"content-type": "application/json",
}
});
c.executionCtx.waitUntil(promise.then())

flow.params = { campaignId: campaignId }
return flow;

however when I am testing it local and it is not hitting the service I am using hono https://hono.dev/api/context#executionctx the worker is on localhost:8787 and the write service for the impression is on localhost:3000 I am able to create the impression successfully calling it directly but the waitUntil is not hitting localhost:3000 when calling the localhost:8787 endpoint
Context - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
9 Replies
tony
tony4mo ago
Even without the .then() the service is not being called
conqr
conqr4mo ago
Are you using miniflare or remote?
conqr
conqr4mo ago
Cloudflare Docs
Known issues · Cloudflare Workers docs
Known issues and bugs to be aware of when using Workers.
tony
tony4mo ago
The wrangler endpoint (localhost:8787) is being hit fine. I have a successful log to the console of the JSON.stringify(data) It is not however sending the POST to the http://localhost:3000/impression But when sending a POST in postman to http://localhost:3000/impression i get a response. @conqr
conqr
conqr4mo ago
Read the thing above This
tony
tony4mo ago
Even when not testing it locally and in a live environment it is not posting Validated that the enpoint being sent is accepting traffic / getting a response in postman
Chaika
Chaika4mo ago
there's port restrictions with live workers (flowchart by community mvp albert)
No description
Chaika
Chaika4mo ago
same-zone being same website in CF what are you trying to fetch? Does it work without putting it in the ctx.waitUntil? dk if it would error out in there, iirc it would log it but not change the outcome of the request
tony
tony4mo ago
@Chaika It does work when removing the waitUntil:
const response = await fetch(`${DO_URL}impressions`, {
method: "POST",
body: JSON.stringify(data),
headers: {
"content-type": "application/json",
}
})

const imp = await response.json();

console.log("Impression created", JSON.stringify(imp))
// c.executionCtx.waitUntil(promise.then(() => console.log("Impression created")))
const response = await fetch(`${DO_URL}impressions`, {
method: "POST",
body: JSON.stringify(data),
headers: {
"content-type": "application/json",
}
})

const imp = await response.json();

console.log("Impression created", JSON.stringify(imp))
// c.executionCtx.waitUntil(promise.then(() => console.log("Impression created")))
"logs": [
{
"message": [
" <-- GET /c/efe826eb-5263-482a-8291-7c3052e3469c"
],
"level": "log",
"timestamp": 1714494689412
},
{
"message": [
"Impression created",
"[object Object]"
],
"level": "log",
"timestamp": 1714494690525
},
{
"message": [
" --> GET /c/efe826eb-5263-482a-8291-7c3052e3469c \u001b[36m301\u001b[0m 1s"
],
"level": "log",
"timestamp": 1714494690525
}
]
"logs": [
{
"message": [
" <-- GET /c/efe826eb-5263-482a-8291-7c3052e3469c"
],
"level": "log",
"timestamp": 1714494689412
},
{
"message": [
"Impression created",
"[object Object]"
],
"level": "log",
"timestamp": 1714494690525
},
{
"message": [
" --> GET /c/efe826eb-5263-482a-8291-7c3052e3469c \u001b[36m301\u001b[0m 1s"
],
"level": "log",
"timestamp": 1714494690525
}
]
well.. it does give a log, but when stringifying it shows an empty object. investigating I am offloading a POST to another service to since it is not needed in the worker response. got the request working updated the code in the message above. Okay this is now working working code:
const promise = fetch(`${DO_URL}impressions`, {
method: "POST",
body: JSON.stringify(data),
headers: {
"content-type": "application/json",
}
})

c.executionCtx.waitUntil(promise.then(async (response) => {
const imp = await response.json()
console.log("Impression created", JSON.stringify(imp))
}))
const promise = fetch(`${DO_URL}impressions`, {
method: "POST",
body: JSON.stringify(data),
headers: {
"content-type": "application/json",
}
})

c.executionCtx.waitUntil(promise.then(async (response) => {
const imp = await response.json()
console.log("Impression created", JSON.stringify(imp))
}))