Closed mikkelhegn closed 8 months ago
I've tried the following, which also results in same values being returned for every execution:
let number = Math.floor(Math.random() * 10)
export const handleRequest: HandleRequest = async function (request: HttpRequest): Promise<HttpResponse> {
const requestId = uuidv4()
return {
status: 200,
headers: { "content-type": "text/plain" },
body: JSON.stringify({ id: requestId, n: number})
}
}
Even when I declare a variable outside the handler (either using Math.floor(Math.random() * 10)
or uuidv4()
) that is NOT used at all, it keeps on returning the same values across multiple requests:
let notUsedAtAll = Math.floor(Math.random() * 10)
export const handleRequest: HandleRequest = async function (request: HttpRequest): Promise<HttpResponse> {
const requestId = uuidv4()
let number = Math.floor(Math.random() * 10)
return {
status: 200,
headers: { "content-type": "text/plain" },
body: JSON.stringify({ id: requestId, n: number})
}
}
However, defining a third variable, which is unrelated to some sort of randomness, it works as expected:
let bar = "baz"
export const handleRequest: HandleRequest = async function (request: HttpRequest): Promise<HttpResponse> {
const requestId = uuidv4()
let number = Math.floor(Math.random() * 10)
return {
status: 200,
headers: { "content-type": "text/plain" },
body: JSON.stringify({ id: requestId, n: number, foo: bar})
}
}
This has the same underlying cause as https://github.com/fermyon/spin-python-sdk/issues/55, and will require the same solution. I'll open a PR later today.
The code below result in Math.random() always returning the same number, on each request. Moving the constant requestId in to the handler function resolves it, so that Math.random() now returs a new number on each request.