1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How can I force sequential execution on 2 simultaneous requests?

Discussion in 'JavaScript' started by SoftLink, May 30, 2024.

  1. #1
    I've got an input form with 2 inputs.
    Each input value (not the whole form) is sent to the server immediately after it's set.
    I can pretty much make both inputs submit simultaneously.
    I fill in the text box and without leaving (blur) I click a checkbox.

    The server gets the second request before it can process the first.
    The second needs an ID that is generated during the processing of the first.
    The second is so quick that it precedes the generation of the ID so it thinks it's the first and generates another ID.

    Sessions in this case are not possible. The entire client side is javascript.
    There are no session variables. I do create a session.
    The app generates a unique 'session' ID when it receives the first *page* (not form) request from a given visitor and saves it to a db record. It sends the session ID in the response to the client. All subsequent *form* requests from that visitor include the session ID.

    When the server gets the first *form* request from that visitor it generates another (not the session) ID which each subsequent form request from that visitor needs.

    The app fills the session record with the ID as soon as it gets the first form request.
    It's TOO LATE! The server gets the second request before the ID is generated.
    The second doesn't see the new ID, thinks it's the first and generates another ID.

    I can force a delay if there's no ID yet but that's awful sloppy, not efficient.

    How can I guarantee that the second form request will see the new ID that the first generated?
    SoftLink, May 30, 2024 IP
  2. GreenHost.Cloud

    GreenHost.Cloud Member

    Likes Received:
    Best Answers:
    Trophy Points:
    To make sure things happen in the right order, you could set up a system in JavaScript where the second input is only sent once the first input's ID is ready. This ensures that the server gets the information it needs in the right order. Another option is to use async requests and manage how the two inputs work together. It's also important to make sure the server code knows how to handle IDs in order to prevent any mix-ups.
    GreenHost.Cloud, Jun 8, 2024 IP
    SoftLink likes this.
  3. sarahk

    sarahk iTamer Staff

    Likes Received:
    Best Answers:
    Trophy Points:
    Look up Promises.
    sarahk, Jun 9, 2024 at 3:49 PM IP
  4. SoftLink

    SoftLink Active Member

    Likes Received:
    Best Answers:
    Trophy Points:
    GreenHost: Thanks for your response.
    The form actually has x number of inputs depending on the usage, usually up to 25.
    I don't care which is 'first', I only care that the 'second' uses the ID generated by the first.
    I can't slow the user's input values at all. He needs to be able to set values as quickly as possible.
    I don't see how I could do this without async. I don't want to delay the second until I get the first.
    I suppose I could create some type of que on the client but I don't like that idea.
    So far I'm delaying on the server 1 whole minute (that's what it took) which *seems* to have solved the problem.
    I don't like it and I'm not convinced it's a guaranteed solution.
    I'm guessing a cue is the only way to go;
    hold the subsequent values in que until the client has the ID.
    I didn't really want to send the ID to the client at all.

    sarahk: Thanks for your response.
    How did you intend for me to use promises? Were you thinking of creating a que on the client? or something else?
    SoftLink, Jun 10, 2024 at 2:21 AM IP
  5. sarahk

    sarahk iTamer Staff

    Likes Received:
    Best Answers:
    Trophy Points:
    Promises let you chain your requests but I'm wondering if the whole work flow needs a peer review.
    sarahk, Jun 10, 2024 at 5:28 PM IP