Add some async to the queries

main
Ibraheem Saleh 10 months ago
parent cc02ef4363
commit d9839f21ea

@ -39,7 +39,7 @@ const headers = {
'TE': 'trailers' 'TE': 'trailers'
}; };
const skuIds = ["6614151","6616096","6617487","6614119","6614120","6616095","6616092","6616096","6614122","6616093","6616090","6616091","6615930","6615929","6615931"] const skuIds = ["6614151","6616096","6617487","6614119","6614120","6616095","6616092","6614122","6616093","6616090","6616091","6615930","6615929","6615931"]
const queries = skuIds.map(skuId => ({ const queries = skuIds.map(skuId => ({
skuId, skuId,
@ -151,9 +151,18 @@ async function fetchGraphQL(zip, store, query) {
async function pollStore(zip, store) { async function pollStore(zip, store) {
console.log(`Starting poll for zip ${zip} and store ${store}...`); console.log(`Starting poll for zip ${zip} and store ${store}...`);
const batchSize = 5;
while (!found) { while (!found) {
for (const { skuId, query } of queries) { const queryBatches = [];
const data = await fetchGraphQL(zip, store, query); for (let i = 0; i < queries.length; i += batchSize) {
queryBatches.push(queries.slice(i, i + batchSize));
}
for (const batch of queryBatches) {
const fetchPromises = batch.map(({ skuId, query }) => fetchGraphQL(zip, store, query).then(data => ({ skuId, data })));
const results = await Promise.all(fetchPromises);
for (const { skuId, data } of results) {
if ( if (
data && data &&
data.data && data.data &&
@ -166,27 +175,28 @@ async function pollStore(zip, store) {
console.log(`skuId: ${skuId} Store: ${store} Ship to: ${zip} -> buttonState: ${buttonState}`); console.log(`skuId: ${skuId} Store: ${store} Ship to: ${zip} -> buttonState: ${buttonState}`);
if (buttonState !== "SOLD_OUT") { if (buttonState !== "SOLD_OUT") {
found = true; found = true;
const productUrl = getProductUrl(skuId) const productUrl = getProductUrl(skuId);
console.log(productUrl) console.log(productUrl);
winningResult = { skuId, zip, store, buttonState, data }; winningResult = { skuId, zip, store, buttonState, data };
const openPromise = open(productUrl); const openPromise = open(productUrl);
const addToCartPromise = addToCart(productUrl); const addToCartPromise = addToCart(productUrl);
await open('./alarm.wav'); await open('./alarm.wav');
await addToCartPromise await addToCartPromise;
return winningResult; return winningResult;
} }
} else { } else {
console.log(`Incomplete data for zip ${zip}, store ${store}.`); console.log(`Incomplete data for zip ${zip}, store ${store}.`);
} }
lines++ lines++;
if(lines > 30) { if (lines > 30) {
lines = 0; lines = 0;
process.stdout.write('\x1B[3J\x1B[2J\x1B[H'); process.stdout.write('\x1B[3J\x1B[2J\x1B[H');
} }
await new Promise(resolve => setTimeout(resolve, 500)); await new Promise(resolve => setTimeout(resolve, 500));
} }
} }
return null; }
return null;
} }
async function runAllPollers() { async function runAllPollers() {

Loading…
Cancel
Save