Skip to content

Instantly share code, notes, and snippets.

@mihailgaberov
Last active July 29, 2022 12:05
Show Gist options
  • Save mihailgaberov/68f02840ebf01c819f2bb2332dbc3e67 to your computer and use it in GitHub Desktop.
Save mihailgaberov/68f02840ebf01c819f2bb2332dbc3e67 to your computer and use it in GitHub Desktop.
function createGreeter(greeting) {
return function (name) {
console.log(greeting + ", " + name);
};
}
const sayHello = createGreeter("Hello");
sayHello("Joe"); // Hello, Joe
sayHello("Mihail"); // Hello, Mihail
sayHello("Chris"); // Hello, Chris
console.log(sayHello);
// Spread Syntax
const arr = [4, 6, -1, 3, 10, 4];
const max = Math.max(...arr);
console.log(max);
// Rest Syntax
function myFunc(...args) {
console.log(args[0] + args[1]);
}
myFunc(1, 2, 3, 4);
// Callback Functions
function myLogger(arg) {
console.log(arg);
}
function myFuncCb(text, callback) {
setTimeout(function () {
callback(text);
}, 300);
}
myFuncCb("Hello world!", myLogger);
// Promises
const myPromise = new Promise(function (res, rej) {
setTimeout(function () {
if (Math.random() < 0.9) {
return res("Hooray!");
}
return rej("Oh no!");
}, 1000);
});
myPromise
.then(function (data) {
console.log("Success: " + data);
})
.catch(function (err) {
console.log("Error: " + err);
});
// Async Await
const greeterPromise = new Promise((res, rej) => {
setTimeout(() => res("Hello Mihail!"), 250);
});
async function myFuncAsync() {
const greeting = await greeterPromise;
console.log(greeting);
}
myFuncAsync(); // 'Hello world!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment