Hi, I've been working on a project that I want to make better, I had this upgrade idea on it, And it's requesting multiple times from an API at a short time, Now the problem here is that, How can I order the responses according to which request was sent first? In-depth, My code here requests multiple times in a
for loop:
var queue = [];
function Request(text) {
console.log(text);
async function Test(){
await Api.request();
let result = Api.response
var URL = result.Object();
queue.push(URL);
var I = queue.shift();
console.log(I)
}
Test()
}
for (Something) {
setTimeout(function() {
Request(text);
}, 2000);
}
This is my code that requests from an API multiple times according to the for loop rule, For this sake, I made it clear as an example and not real API functions etc...
Let's say that the rule for the
for loop here makes it that the API returns 3 responses
Response1 // Took 1 second to respond
Response3 // Took 1.5 seconds to respond
Response2 // Took 2 seconds to respond
Notice that the responses are not ordered accordingly, And they're randomized, Since Response3 Could be faster than Response2 and the examples goes on....
Now of course, Each response depending on the API will sometimes get eariler/later than the others, Which makes the problem here and my question here is that:
How can I know that this response came earlier/later but belongs to the first request, And same goes to the 2 other requests.
Request1: Response 1 // Took 1 second to respond
Request2: Response 2 // Took 2 seconds to respond
Request3: Response 3 // Took 1.5 seconds to respond
It doesn't matter if the code have to wait for Response 2 to come before Response 3 even though Response 3 came before Response 2.
I hope this made any sense, I'm sorry for my bad English. Thanks in advance.
(EDIT): Since it's too hard to explain what I'm trying to achieve, I'll post an actual piece of my code here:
var text = "How's work this week? | It's been pretty good not going to lie. It has been a pretty decent day as well, I like how everything is going this week, | I hope it get's better."
const API = require('SomeApiWrapperLibrary.js');
var queue = [];
async function processText(text) {
var Object1 = "Something"
const Rw = new Api.Client({
usernameOrEmail: "something",
password: "somethingelse"
});
async function Test(){
console.log(text);
await Rw.start();
let result = await Rw.makeTTS(voice, text);
var URL = result.Object();
queue.push( text + " : " + URL);
var I = queue.shift();
console.log(I)
}
Test()
}
const prefix = ' | ';
const splitText = text.split(prefix);
async function Request(){
for (const text of splitText) {
await processText(text).then(console.log('Done'))
}
}
Request()
What I have tried:
What I tried here is to number-ize the response that's being pushed to the queue as you can see here:
var queue = [];
var Number = 0;
function Request(text) {
console.log(text);
async function Test(){
await Api.request();
let result = Api.response
Number++
var URL = result.Object();
queue.push( "URL " + Number + URL);
var I = queue.shift();
console.log(I)
}
Test()
}
for (Something) {
setTimeout(function() {
Request(text);
}, 2000);
}
But that's totally wrong because the number here Will always be from 1 to 3, Even if let's say the 2nd response was meant to be the first one, It'll still be called 2, If that makes any sense.