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
// Multiple sources with the callback pattern | |
getData((a) => ( | |
getMore(a, b => ( | |
andEvenMore(b, c=> ( | |
andLastlyMore(c, d => ( | |
// do something with d | |
) | |
) | |
) | |
); |
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
Promise.All( [somePromise, anotherPromise, andAFinalPromise] ).then( values => { | |
//values is an array with the resolves from each of the promises. | |
}).catch( (err) => { | |
// if one of the promises rejects, the whole fails. | |
}); | |
Promise.race( [somePromise, anotherPromise, andAFinalPromise] ).then ( value => { | |
// value is a single value from the promise that resolved or rejected first. | |
}); |
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
let someFunction = () => { | |
let combined = ``; | |
callSomePromise.then( (val) => { | |
return combined + val; | |
} | |
return combined; | |
} |
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
let myPromise = new Promise( (res, rej) => { | |
// rather than this function returning, it resolves `res`, or rejects `rej` | |
if(someCrazyProcess){ | |
res(`it worked`); | |
} else { | |
rej(`it didn't work`) | |
} | |
}); | |
// then to use the promise |
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
myAjaxCall({ | |
success: (data) => { /* do something with the data */ }, | |
failure: (err) => { console.error(`something went wrong`, err); | |
}); |
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
const someFunction = (callback) => { | |
//do some stuff | |
callback(); | |
} | |
someFunction( () => { | |
// something to run that is getting passed into the function I am calling. | |
}); |
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
const someReallyAwesomeFunction = () => { | |
//some process that you want to know when it is finished in an async way. | |
//After we are finished with the process, then we can push an event | |
// with custom events we can attach what ever data we need to pass. | |
let myEvent = new CustomEvent('customEventName', { | |
detail: { | |
obj: 'that you can pass with the event!', | |
pass: 'what ever you want' |
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
// in the DOM | |
someElement.addEventListener('someEvent', someHandler); | |
// Simple familiar example that removes the element when it is clicked. | |
someElement.addEventListener('click', (e) => e.target.remove()); |
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
// Explicit return | |
let someFunction = prop => { | |
return someValue; | |
}; | |
// Implicit return | |
let someFunction = prop => someValue; | |
// multiline implicit return | |
let someFunction = prop => ( |
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
let someFunction = prop => { | |
}; |