Skip to content

Instantly share code, notes, and snippets.

// Multiple sources with the callback pattern
getData((a) => (
getMore(a, b => (
andEvenMore(b, c=> (
andLastlyMore(c, d => (
// do something with d
)
)
)
);
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.
});
let someFunction = () => {
let combined = ``;
callSomePromise.then( (val) => {
return combined + val;
}
return combined;
}
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
myAjaxCall({
success: (data) => { /* do something with the data */ },
failure: (err) => { console.error(`something went wrong`, err);
});
const someFunction = (callback) => {
//do some stuff
callback();
}
someFunction( () => {
// something to run that is getting passed into the function I am calling.
});
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'
// 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());
// Explicit return
let someFunction = prop => {
return someValue;
};
// Implicit return
let someFunction = prop => someValue;
// multiline implicit return
let someFunction = prop => (
let someFunction = prop => {
};