Skip to content

Instantly share code, notes, and snippets.

@debuging-life
Created January 8, 2020 10:09
Show Gist options
  • Select an option

  • Save debuging-life/ab5feb75264853e897aa59cdda1c9b66 to your computer and use it in GitHub Desktop.

Select an option

Save debuging-life/ab5feb75264853e897aa59cdda1c9b66 to your computer and use it in GitHub Desktop.
PWA
var deferrdPrompt;
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/sw.js")
.then(function() {
console.log("service worker register");
})
.catch(function(error) {
console.log(error);
});
}
window.addEventListener("beforeinstallprompt", function(event) {
console.log("fired");
event.preventDefault();
deferrdPrompt = event;
return false;
});
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
reject({ code: 500, message: "An error occurred" });
// resolve("this is executed once the timer is done");
}, 3000);
});
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://httpbin.org/ip');
xhr.onload = function() {
console.log(xhr.response);
}
xhr.onerror = function {
console.log('Error!');
}
xhr.send();
fetch("https://httpbin.org/ip")
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data);
})
.catch(function(error) {
console.log(error);
});
fetch("https://httpbin.org/post", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
mode: "cors",
body: JSON.stringify({ message: "Doest this work?" })
})
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data);
})
.catch(function(error) {
console.log(error);
});
promise
.then(function(value) {
return value;
})
.then(resp => {
console.log(resp);
})
.catch(function(e) {
console.log(e);
});
console.log("this excute right after settime out");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment