Search
 
SCRIPT & CODE EXAMPLE
 

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)
Comment

PREVIOUS NEXT
Code Example
Javascript :: Calculating with Functions 
Javascript :: destructuring array es6 
Javascript :: How to Loop Through an Array with a do…while Loop in JavaScript 
Javascript :: Backbone Template 
Javascript :: mongoose export collection 
Javascript :: get data from multiple api in react 
Javascript :: get the first recurring character javascript 
Javascript :: slicer 
Javascript :: send form data to endpoint js 
Javascript :: how to change array elements position in array in javascript 
Javascript :: add even javascript 
Javascript :: xor two hex strings js 
Javascript :: 2d array js 
Javascript :: how to print reverse number in javascript 
Javascript :: flutter webview javascript 
Javascript :: change your favicon in javascript 
Javascript :: change string to int javascript 
Javascript :: react how to get checkbox value on click 
Javascript :: dart to json 
Javascript :: react component pass props 
Javascript :: id multiple same property array combining 
Javascript :: arjs marker+location 
Javascript :: useScrollPrecent 
Javascript :: javascript Access String Characters 
Javascript :: javascript for...of with Maps 
Javascript :: JavaScript HTML DOM Navigation 
Javascript :: GetAsync() with a dateime 
Javascript :: divide array in chunks 
Javascript :: Elementor Hide Sticky Header on Scroll Down - Show on Scroll Up 
Javascript :: phaser play animation after repeat 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =