Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Watch an API for Updates

const sleep = ms => new Promise(res => setTimeout(res,  ms));

async function mockNetworkCall(num) { 
  const timings = [50, 150, 50]
  console.log(`sending request ${num}`);
  await sleep(timings[num]);
  
  console.log(`request ${num} finished`)
}


for(let i = 0; i < 3; i++) {
  mockNetworkCall(i);
}


// if you use nodejs express module // 
// create async timeout function
const sleep = ms => Promise(res => setTimeout(res, ms));
// create api watcher ( requests loop ) function 
async function watchApi(url){
  	try{
      const data = await fetch(url);
      // do anything with the here or with the data 
    }catch{
      console.log("error");
    }
	
  	await sleep(5000);
  	await watchApi(url);
}

// execute the function
watch(url)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Watch #API #Updates
ADD COMMENT
Topic
Name
9+6 =