Skip to content

Math.random() always returns the same number, when using the uuid library and instantiating uuidv4() outside the handler function #215

@mikkelhegn

Description

@mikkelhegn

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.

import { HandleRequest, HttpRequest, HttpResponse } from "@fermyon/spin-sdk"
import { v4 as uuidv4 } from "uuid";

// Moving this inside the handler will solve the problem
const requestId = uuidv4()

export const handleRequest: HandleRequest = async function (request: HttpRequest): Promise<HttpResponse> {

  let number = Math.floor(Math.random() * 10)

  console.log(number)

  return {
    status: 200,
    headers: { "content-type": "text/plain" },
    body: JSON.stringify(number)
  }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions