const getTodos = (callback) => { // FUNCTION TO MAKE REQUEST const request = new XMLHttpRequest(); request.addEventListener('readystatechange', () =>{ if(request.readyState === 4 && request.status === 200){ callback(undefined, request.responseText) }else if(request.readyState === 4){ callback('could not fetch data', undefined) } }); request.open('GET', 'https://jsonplaceholder.typicode.com/todos'); request.send(); } getTodos((err, data) => { // REUSABLE FUNCTION TO CHECK DATA console.log('callback') if(err){ console.log(err) }else{ console.log(data) } })
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter