I try to create a loading test script but I faced with weird behaviour I use https://github.com/prisma-labs/graphql-request module for graphql request.
But I don't see that the process run into the callback
I see the next result. It stoped on the 5 first items.
GET: MX
GET: MX
GET: DE
GET: LT
GET: US
I think maybe I don't have access to the requestCount variable in the callback function but I don't even see that the request was sent.
import { request, gql } from 'graphql-request'
const MAX_REQUESTS = 5;
const countries = ['GB', 'UK', 'US', 'LT', 'IT', 'DE', 'MX']
const getRandomElement = (items) => {
return items[Math.floor(Math.random()*items.length)];
}
let requestCount = 0;
while (true) {
let country = getRandomElement(countries);
let query = gql`
{
country(code: "${country}") {
name
native
capital
emoji
currency
languages {
code
name
}
}
}`
if (requestCount >= MAX_REQUESTS) {
continue;
}
console.log('GET: ', country);
request('https://countries.trevorblades.com/', query).then((data) => {
console.log('OK: ', country);
requestCount--;
}).catch((error) => {
console.log('FAILED: ', country);
requestCount--;
console.error(error)
})
requestCount++;
}