Skip to content

Instantly share code, notes, and snippets.

@albacoretuna
Last active November 16, 2016 16:10
Show Gist options
  • Save albacoretuna/9cc21886f227811becbac797d8e2d710 to your computer and use it in GitHub Desktop.
Save albacoretuna/9cc21886f227811becbac797d8e2d710 to your computer and use it in GitHub Desktop.
/*
var async = true;
var xhr = new XMLHttpRequest();
xhr.open('get', 'favicon.ico', async);
xhr.send();
var timestamp = Date.now() + 3000;
while(Date.now() < timestamp);
function listener(msg) {
console.log('Greetings from listner ', msg);
}
xhr.addEventListener('error', listener);
xhr.addEventListener('load', listener);
*/
// request
/*
function repositionElement() {
console.log('repositioning..');
}
window.requestAnimationFrame(repositionElement);
console.log('I am the last line of the script');
*/
/*
var async = true;
var xhr = new XMLHttpRequest();
xhr.open('get', 'data.json', async);
xhr.send();
setTimeout(function delayed() {
// Creates race condition!
function listener() {
console.log('greetings from listener');
}
xhr.addEventListener('load', listener);
xhr.addEventListener('error', listener);
}, 550);
*/
/*
// loadImage, callback style
function loadImage(url, success, error) {
var img = new Image();
img.src = url;
img.onload = function() {
success(img);
}
;
img.onerror = function(e) {
error(e);
}
;
}
loadImage('http://omid.fi/wiki/lib/tpl/dokuwiki/images/logo.png', function onsuccess(img) {
// Add the image to the current web page
document.body.appendChild(img);
}, function onerror(e) {
console.log('Error occurred while loading image');
console.log(e);
});
*/
/*
// loadImage promise style
function loadImage(url) {
var promise = new Promise(function resolver(resolve, reject) {
var img = new Image();
img.src = url;
img.onload = function() {
resolve(img);
}
;
img.onerror = function(e) {
reject(e);
}
;
}
);
return promise;
}
var wikiImage = 'https://www.safaribooksonline.com/library/view/javascript-with-promises/9781491930779/assets/jswp_0201.png';
loadImage(wikiImage).then(function(img) {
document.body.appendChild(img);
console.log('yes got added!')
}).catch(function(e) {
console.log('error tuli!');
console.log(e);
});
*/
// promise state is set only once!
/*
var promise = new Promise(function (resolve, reject) {
resolve(Math.PI);
reject(0);
resolve(Math.sqrt(-1));
});
promise.then(function (number) {
console.log('The number is ' + number);
});
*/
// Promise.resolve('some value') and Promise.reject('some reason') are
// shorthands to resolve / reject a promise quickly
/*
var promise = new Promise(function (resolve, reject) {
resolve(Promise.resolve(Math.PI));
reject(0);
resolve(Math.sqrt(-1));
});
promise.then(function (number) {
console.log('The number is ' + number);
});
new Promise(function (resolve, reject) {
resolve ('the long way');
});
*/
// example 2-8 call to then always returns a new promise
/*
var p1, p2;
p1 = Promise.resolve();
p2 = p1.then(function() {});
console.log('p1 and p2 are different objects: ' + (p1 !== p2));
*/
// passing values in a sequence of steps
/*
Promise.resolve('tada!').then(
function step2(result) {
console.log('Step 2 received ' + result);
return 'Greetings from step 2';
}
).then(
function step3(result) {
console.log('Step 3 recieved '+ result);
}
).then(
function step4(result) {
console.log('step 4 recieved ' + result);
return Promise.resolve('Fulfilled value');
}
).then(
function step5(result){
console.log('step 5 recieved ' + result);
}
);
*/
// execution order of callbacks used by promises
/*
var promise = new Promise(function (resolve, reject){
console.log('Inside the resolver function');
resolve();
});
promise.then(function () {
console.log('Inside the onFulFilled handlre');
});
console.log('This is the last line of script');
*/
// Error handling
/*
Promise.reject(Error('bad news')).then(
function step2() {
console.log('This is never run');
}
).then(
function step3() {
console.log('This is also never run ');
}
).catch(
function error(err) {
console.log('something failed along the way, Inspect error for more info');
console.log(err);
}
);
*/
function rejectWith (val) {
return new Promise(function (resolve, reject) {
throw Error(val);
resolve('Not used');
});
}
rejectWith('bad news').then(
function step2() {
console.log('This is never run');
}
).catch(
function(error) {
console.log('Failed again!');
console.log(error);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment