├── .babelrc ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public └── index.html ├── src ├── README.md ├── index.js └── recursion.js └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | [ "@babel/preset-env", { 4 | "modules": false, 5 | "targets": { 6 | "browsers": [ 7 | "last 2 Chrome versions", 8 | "last 2 Firefox versions", 9 | "last 2 Safari versions", 10 | "last 2 iOS versions", 11 | "last 1 Android version", 12 | "last 1 ChromeAndroid version", 13 | "ie 11" 14 | ] 15 | }, 16 | "useBuiltIns": "usage" 17 | } ] 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.log 3 | npm-debug.log 4 | yarn-error.log 5 | .DS_Store 6 | build/ 7 | node_modules/ 8 | dist/ 9 | .cache 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JavaScript Concepts :mortar_board: 2 | 3 | ## Description :clipboard: 4 | > A demo app for JavaScript Concept 5 | 6 | ## Installation :wrench: 7 | 8 | 1. Clone this repo by running `git clone https://github.com/imranhsayed/javascript-concepts` 9 | 2. `cd javascript-concepts` 10 | 3. `cd branch-name` 11 | 4. `npm install` 12 | 5. `npm run server` 13 | 14 | ## Branch Information :link: 15 | 16 | #### 1. [callbacks]() 17 | 18 | * Callbacks add complexity and readability issue. And its messy to chain the tasks 19 | 20 | #### 2. [promises](https://github.com/imranhsayed/javascript-concepts/tree/promises) 21 | 22 | * A promise is an object that represent the eventual( future ) completion(or failure) of an asynchronous operation, and its future result value. 23 | Promises are thin abstraction around call backs. ( e.g. `readFile.( 'config.json' ).then(...).catch(...)` ) 24 | In below example what you pass in resolve will be available as value in `.then(( value )=> ...)` 25 | 26 | ```ruby 27 | var promise1 = new Promise(function(resolve, reject) { 28 | setTimeout(function() { 29 | resolve('foo'); 30 | }, 300); 31 | }); 32 | 33 | promise1.then(function(value) { 34 | console.log(value); 35 | // expected output: "foo" 36 | }); 37 | ``` 38 | 39 | 3. [async-await](https://github.com/imranhsayed/javascript-concepts/tree/async-await) 40 | 41 | ##### Async - declares an asynchronous function `(async function someName(){...})`. 42 | * Automatically transforms a regular function into a Promise. 43 | * When called async functions resolve with whatever is returned in their body. 44 | * Async functions enable the use of `await`. 45 | 46 | ##### Await - pauses the execution of async functions. `(const result = await someAsyncCall();)`. 47 | * When placed in front of a Promise call, await forces the rest of the code to wait until that Promise finishes and returns a result. 48 | * Await works only with Promises, it does not work with callbacks. 49 | * Await can only be used inside async functions. 50 | 51 | ##### Async Await Example 52 | 53 | ```ruby 54 | async function getTodoData() { 55 | const todoData = await fetch( 'https://jsonplaceholder.typicode.com/todos/' ); 56 | return todoData; 57 | } 58 | 59 | getTodoData() 60 | .then( res => res.json() ) 61 | .then( ( result ) => { 62 | console.warn( result ); 63 | } ); 64 | ``` 65 | 66 | 67 | ##### Try and Catch Method 68 | 69 | ```ruby 70 | async function getPosts() { 71 | try { 72 | const postsData = await await fetch( 'https://jsonplaceholder.typicode.com/posts/' ); 73 | return postsData; 74 | } 75 | catch ( error ) { 76 | console.warn( 'error', error ); 77 | } 78 | } 79 | 80 | getPosts() 81 | .then( res => res.json() ) 82 | .then( ( result ) => { 83 | console.warn( result ); 84 | } ); 85 | 86 | ``` 87 | 88 | ## Fetch Example 89 | ```ruby 90 | fetch('https://example.com/wp-json/wp/v2/posts') 91 | .then( 92 | function(response) { 93 | if (response.status !== 200) { 94 | console.log('Looks like there was a problem. Status Code: ' + 95 | response.status); 96 | return; 97 | } 98 | 99 | // Examine the text in the response 100 | response.json().then(function(data) { 101 | console.log(data); 102 | }); 103 | } 104 | ) 105 | .catch(function(err) { 106 | console.log('Fetch Error :-S', err); 107 | }); 108 | ``` 109 | 110 | ## Useful Links :link: 111 | 112 | 1. [async-await concept blog](https://tutorialzine.com/2017/07/javascript-async-await-explained) 113 | 2. [try-catch](https://www.w3schools.com/js/js_errors.asp) 114 | 3. [Promise.all()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) 115 | 116 | ## Instructions :point_right: 117 | 118 | ## Common Commands :computer: 119 | 120 | 1. `npm run dev` starts a webpack dev server at [http://localhost:8080](http://localhost:8080) 121 | 2. `npm run prod` runs build for production in dist directory. 122 | 123 | ## Built With :zap: 124 | 125 | 1. Node 126 | 2. Express 127 | 3. ES6 128 | 4. Webpack 129 | 5. Babel 130 | 131 | ## Author :bust_in_silhouette: 132 | 133 | * **[Imran Sayed](https://codeytek.com)** 134 | 135 | ## License 136 | 137 | [](http://badges.mit-license.org) 138 | 139 | - **[MIT license](http://opensource.org/licenses/mit-license.php)** 140 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "javascript-concepts", 3 | "version": "1.0.0", 4 | "description": "JavaScript Concept Examples", 5 | "main": "index.js", 6 | "scripts": { 7 | "webpack-dev-server": "webpack-dev-server", 8 | "dev": "webpack-dev-server --mode=development", 9 | "prod": "webpack --mode=production" 10 | }, 11 | "keywords": [ 12 | "javascript" 13 | ], 14 | "author": "Imran Sayed", 15 | "license": "ISC", 16 | "dependencies": { 17 | "@babel/core": "^7.4.5", 18 | "@babel/polyfill": "^7.4.4", 19 | "@babel/preset-env": "^7.4.5", 20 | "babel-loader": "^8.3.0", 21 | "html-webpack-plugin": "^5.5.0", 22 | "webpack": "^5.75.0", 23 | "webpack-cli": "^5.0.1", 24 | "webpack-dev-server": "^3.4.1" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |