UNPKG

js-102

Version:

JS-102 helps you learn JavaScript (the right way) so that you can confidently use higher-level libraries and frameworks. — Let’s reveal the magic!

133 lines (100 loc) 3.66 kB
/* * the devil is in the details * .--. __--__ (`-') .--. .----. .----. * | ,|/ _ / ( OO).->/_ | / .. \\_,-. | * |(_|\_..`--.(,------. | || / \ . .' .' * ,--. | |.-._) \`------' | |' \ / '.' /_ * | '-' /\ / | | \ `' /| | * `-----' `-----' `--' `---'' `------' * * This project is a part of the “Byte-Sized JavaScript” videocasts. * * You can watch “Byte-Sized JavaScript” at: https://bytesized.tv/ * * MIT Licensed — See LICENSE.md * * Send your comments, suggestions, and feedback to me@volkan.io */ /* eslint-ignore */ // ## Note // // The code you see here is just a pseudocode to show different programming // paradigms. When you try to run it, it will not run. // // Watch (Episode 18)* for the screncast that covers this topic. // // * https://bytesized.tv/episodes/18/byte-sized-javascript-episode-18-all-the-promises-we-made/FiTXJQWilDM // // This is how you bake a cake sequentially: // const bakeCheesecake = () => { const ingredients = fetchIngredients(); const oven = locateOven(); oven.heat( '325F' ); waitUntil( oven.ready ); const batter = join( ingredients.grahamCrumbs, ingredients.sugar.take( '3tbsp' ) ); saucepan.spread( batter ); mixture = beat( ingredients.creamCheese, ingredients.vanilla, ingredients.sugar.take( '1cup' ) ); waitUntil( mixture.blended ); oven.put( mixture ); oven.bake( '55m' ); waitUntil( oven.timer.remaining.equals( 0 ) ); console.log( 'Your cheesecake is ready. Yum!' ); } // // This is how you bake a cake using continuation-passing style: // const bakeCheesecakeCps = ( cb ) => { const oven = locateOven(); fetchIngredients( err, ( ingredients ) => { if ( err ) { cb( 'Error' ); return; } heat( oven, '325f', ( err ) => { if ( err ) { cb( 'Error'); return; } const batter = join( ingredients.grahamCrumbs, ingredients.sugar.take( '3tbsp' ) ); spread( saucepan, batter, ( err ) => { if ( err ) { cb( 'Error' ); return; } beat( ingredients.creamCheese, ingredients.vanilla, ingredients.sugar.take( '1cup' ), ( err, mixture ) => { if ( err ) { cb( 'Error' ); return; } bake( oven, mixture, '55m', ( err ) => { if ( err ) { cb( 'Error' ); return; } cb( 'Your cheesecake is ready. Yum!' ); } ); } ); } ); } ); } ); } // // This is how you bake a cake using promises: // const bakePromise = () => { const oven = locateOven(); fetchIngredients() .then( ( ingredients ) => spread( saucepan, join( ingredients.grahamCrumbs, ingredients.sugar.take( '3tbsp' ) ) ) ) .then( ( mixture ) => bake( oven, mixture, '55m' ) ) .then( () => console.log( 'Your cheesecake is ready. Yum!' ) ) .catch( () => console.error( 'Oh poop!' ) ); } // // This is how you bake a cake using async/await: // const bakeAwait = () => { try { const oven = locateOven(); const ingredients = await fetchIngredients(); const mixture = await = spread( saucepan, join( ingredients.grahamCrumbs, ingredients.sugar.take( '3tbsp' ) ) ); await bake( oven, mixture, '55m' ); console.log( 'Your cheesecake is ready. Yum!' ); } catch ( err ) { console.err( 'Oh poop!' ); } }