Last active
March 18, 2019 12:25
-
-
Save jorenbroekema/2f6958c484b2f12152c369b6dd77a922 to your computer and use it in GitHub Desktop.
Lesson Arrow functions, promises and async / await
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arrow functions | |
let addFunction = function(a, b) { | |
return a + b; | |
} | |
// these are different ways to write functions, with only very minor differences | |
addFunction = function(a, b) { return a + b }; | |
addFunction = (a, b) => { return a + b }; | |
addFunction = (a, b) => a + b ; | |
addFunction = a => a + 1 ; | |
console.log(addFunction(4, 18)); | |
// Most common arrow function | |
addFunction = (a, b) => { | |
// some extra stuff happens here | |
return a + b; | |
}; | |
// which is the same as | |
addFunction = function(a, b) { | |
// some extra stuff happens here | |
return a + b; | |
} | |
// Promises | |
let promise = new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve("done!"); | |
}, 2000); | |
}); | |
console.log(promise); | |
promise.then(function(data) { | |
console.log(data) | |
console.log(promise); | |
}); | |
// Arrow functions make things easier! | |
let promise = new Promise((resolve, reject) => { | |
setTimeout(() => resolve("done!"), 2000); | |
}); | |
console.log(promise); | |
promise.then(data => { | |
console.log(data) | |
console.log(promise); | |
}); | |
// We can also reject! | |
let promise = new Promise((resolve, reject) => { | |
setTimeout(() => reject("rejected!"), 2000); | |
}); | |
console.log(promise); | |
promise.catch(data => { | |
console.log(data) | |
console.log(promise); | |
}); | |
// If you want to do something after a promise is no longer pending but you don't know if it will be resolved or rejected you can use 'finally' | |
// Feel free to use .then, .catch and .finally in a combination, you can chain them like this: | |
promise | |
.then(data => console.log(`Success: ${data}`)) | |
.catch(data => console.log(`Failure: ${data}`)) | |
.finally(() => console.log('We\'re done with this promise :)')); | |
// Chained promises | |
// Sometimes you wait for a promise to resolve, which will then fire another promise, and then you wait for this, and you get something like this: | |
let myPromise = new Promise((resolve, reject) => { | |
setTimeout(() => resolve('done'), 2000); | |
}); | |
let myOtherPromise = new Promise((resolve, reject) => { | |
setTimeout(() => reject(new Error('Something went wrong oh no :\'(')), 4000); | |
}); | |
console.log('Start!'); | |
myPromise.then(() => { | |
myOtherPromise.then(result => { | |
console.log(result); | |
}).catch(error => { | |
// Alternatively, remove the catch block to let the error throw and terminate your program | |
console.warn(error); | |
}).finally(() => console.log('finally done with these promises')); | |
}); | |
// Insert AND THEN gif | |
// We have a nice method of making this easier called async / await | |
mySynchronousFunction = () => { | |
// do something | |
} | |
myAsynchronousFunction = async () => { | |
console.log('Start!'); | |
await myPromise; | |
try { | |
let result = await myOtherPromise; | |
} catch (error) { | |
// Alternatively, remove the catch block to let the error throw and terminate your program | |
console.warn(error); | |
} finally { | |
console.log('finally done with all these promises!'); | |
} | |
} | |
myAsynchronousFunction(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment