asynchronous in javascript code example
Example 1: currying in javascript
function volume(w, h, l) {
return w * h * l;
}
volume(4, 6, 3);
function volume(w) {
return function(h) {
return function(l) {
return w * h* l;
}
}
}
volume(4)(6)(3);
Example 2: asynchronous javascript
console.log ('Starting');
let image;
fetch('coffee.jpg').then((response) => {
console.log('It worked :)')
return response.blob();
}).then((myBlob) => {
let objectURL = URL.createObjectURL(myBlob);
image = document.createElement('img');
image.src = objectURL;
document.body.appendChild(image);
}).catch((error) => {
console.log('There has been a problem with your fetch operation: ' + error.message);
});
console.log ('All done!');
Example 3: javascript this in settimeout
function func() {
this.var = 5;
this.changeVar = function() {
setTimeout(() => {
this.var = 10;
}, 1000);
}
}
var a = new func();
a.changeVar();