currying in javascript
//No currying
function volume(w, h, l) {
return w * h * l;
}
volume(4, 6, 3); // 72
//Currying
function volume(w) {
return function(h) {
return function(l) {
return w * h* l;
}
}
}
volume(4)(6)(3); // 72
currying in javascript
//No currying
function volume(w, h, l) {
return w * h * l;
}
volume(4, 6, 3); // 72
//Currying
function volume(w) {
return function(h) {
return function(l) {
return w * h* l;
}
}
}
volume(4)(6)(3); // 72
javascript this in settimeout
function func() {
this.var = 5;
this.changeVar = function() {
setTimeout(() => { //Don't use function, use arrow function so 'this' refers to 'func' and not window
this.var = 10;
}, 1000);
}
}
var a = new func();
a.changeVar();
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!');
Asynchronous in javascript
//Asynchronous JavaScript
console.log('I am first');
setTimeout(() => {
console.log('I am second')
}, 2000)
setTimeout(() => {
console.log('I am third')
}, 1000)
console.log('I am fourth')
//Expected output:
// I am first
// I am fourth
// I am third
// I am second
Asynchronous in javascript
//Asynchronous JavaScript
console.log('I am first');
setTimeout(() => {
console.log('I am second')
}, 2000)
setTimeout(() => {
console.log('I am third')
}, 1000)
console.log('I am fourth')
//Expected output:
// I am first
// I am fourth
// I am third
// I am second
How does asynchronous javascript work?
const printProcess = () => {
console.log('it will print 2nd');
var currentTime = new Date().getTime();
while (currentTime + 3000 >= new Date().getTime());
console.log('it will print after added 3 second with current time')
}
console.log('it will print 1st ');
printProcess(); //follow arrow funtion after 1st print
console.log('it will print at the end');
//Expected output below:
// it will print 1st
// it will print 2nd
// it will print after added 3 second with current time
// it will print at the end
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us