Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
how to wait for the request to be sent and then send a new request?

i have a problem. im creating a chat app with react and react redux and im using supabase library for sending request to my supabase database. now for sending users entered message, i have a problem. when user enters ten messages together at the same time, those ten messages wont be sent to the database correctly and maybe the last message will be sent correctly. now to fix this problem, i want to implement this: if user enter his/her message, if an other request is sending at the same time, wait for that request to be sent. when that request is sent, send the new request. so when user enter 10 messages for example, the messages will be sent to the database one by one. each request wait for the sending request and when that request is sent to database successfully or it got an error, then send the next pending request. i tried to implement this but i couldnt. because the function that is sending the users message to the database is an event handler and i dont know how to wait for the sending request thre. here is that functions codes:

      //a function to store users message in database 
  const sendMessageHandler = async (e) => {
    e.preventDefault();
    
    //here i want to check if an other request is sending, wait for that request to be sent and 
    then send the new request.
     
    //create an object with users message infos
    const userMessage = {
      from: username,
      text: messageRef.current.value,
      time: `${new Date().getHours()}:${new Date().getMinutes()}`,
    };

    //create an id with the value of current chats length
    const id = chats[currentIndex].chats.length;

    //store the users message in sending messages state
    dispatch(
      chatsActions.addSendingChat({
        ...userMessage,
        id,
        name: chats[currentIndex].name,
      })
    );

    //create the current messages object to update the group row
    let currentChats = {
      name: chats[currentIndex].name,
      users: chats[currentIndex].users,
      onlines: chats[currentIndex].onlines,
      chats: chats[currentIndex].chats,
      unreads: chats[currentIndex].unreads,
    };

    //add users message to current chats
    currentChats.chats = [...currentChats.chats, userMessage];

    //update the chats of the selected group with the new values
    const { error } = await supabase 
      .from("chats")
      .update(currentChats)
      .eq("id", apiChatId);

    //after updating the row, delete the users message that was in sending messages state from there 
    dispatch(chatsActions.removeSendingChat(id));
  };

thanks for your help.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I have no idea how this is implemented for production but what you can do is:

  1. Push sent message to a queue (do not send yet)
  2. Wait for 100ms (or whatever time)
  3. If new message is pushed into queue within 100ms, reset the timer.
  4. If no new messages is inserted in the queue for 100ms, make a API call and send all of them.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!