├── .eslintignore ├── .gitattributes ├── README.md ├── .npmrc ├── .prettierrc ├── .eslintrc.json ├── src ├── sample1.js ├── sample4.js ├── sample10.js ├── sample2.js ├── sample9.js ├── sample6.js ├── sample5.js ├── sample3.js ├── sample15.js ├── sample17.js ├── sample13.js ├── sample12.js ├── sample16.js ├── sample11.js ├── star-wars.js ├── sample14.js ├── sample7.js └── sample8.js ├── .editorconfig ├── package.json ├── LICENSE └── .gitignore /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # async-await 2 | 3 | > async-await samples. 4 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock = false 2 | progress = false 3 | save-exact = true 4 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "arrowParens": "always", 3 | "singleQuote": true 4 | } 5 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["prettier"], 3 | "plugins": ["prettier"], 4 | "env": { 5 | "es6": true 6 | }, 7 | "parserOptions": { 8 | "ecmaVersion": 8 9 | }, 10 | "rules": { 11 | "prettier/prettier": "error" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/sample1.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | function getPerson(id) { 4 | fetch(`http://swapi.co/api/people/${id}`) 5 | .then((response) => response.json()) 6 | .then((person) => console.log(person.name)); 7 | } 8 | 9 | getPerson(1); 10 | -------------------------------------------------------------------------------- /src/sample4.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | async function getPerson(id) { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | return await response.json(); 6 | } 7 | 8 | getPerson(1).then((person) => console.log(person.name)); 9 | -------------------------------------------------------------------------------- /src/sample10.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | const getPerson = async (id) => { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | return await response.json(); 6 | }; 7 | 8 | getPerson(1).then((person) => console.log(person.name)); 9 | -------------------------------------------------------------------------------- /src/sample2.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | async function getPerson(id) { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | const person = await response.json(); 6 | console.log(person.name); 7 | } 8 | 9 | getPerson(1); 10 | -------------------------------------------------------------------------------- /src/sample9.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | const getPerson = async function(id) { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | return await response.json(); 6 | }; 7 | 8 | getPerson(1).then((person) => console.log(person.name)); 9 | -------------------------------------------------------------------------------- /src/sample6.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | async function getPerson(id) { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | return await response.json(); 6 | } 7 | 8 | // not found 9 | getPerson(0).then((person) => console.log(person)); 10 | -------------------------------------------------------------------------------- /src/sample5.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | async function getPerson(id) { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | return await response.json(); 6 | } 7 | 8 | // undefined 9 | getPerson(0).then((person) => console.log(person.name)); 10 | -------------------------------------------------------------------------------- /src/sample3.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | async function getPerson(id) { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | const person = await response.json(); 6 | return person; 7 | } 8 | 9 | getPerson(1).then((person) => console.log(person.name)); 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [*.md] 13 | max_line_length = off 14 | trim_trailing_whitespace = false 15 | -------------------------------------------------------------------------------- /src/sample15.js: -------------------------------------------------------------------------------- 1 | const StarWars = require('./star-wars'); 2 | 3 | async function loadData() { 4 | const sw = new StarWars(); 5 | 6 | const person = await sw.getPerson(1); 7 | const film = await sw.getFilm(1); 8 | 9 | console.log(person.name); 10 | console.log(film.title); 11 | } 12 | 13 | loadData(); 14 | -------------------------------------------------------------------------------- /src/sample17.js: -------------------------------------------------------------------------------- 1 | const StarWars = require('./star-wars'); 2 | 3 | async function loadData() { 4 | const sw = new StarWars(); 5 | 6 | const [person, film] = await Promise.all([sw.getPerson(1), sw.getFilm(1)]); 7 | 8 | console.log(person.name); 9 | console.log(film.title); 10 | } 11 | 12 | loadData(); 13 | -------------------------------------------------------------------------------- /src/sample13.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | const getPerson = async (id) => { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | return await response.json(); 6 | }; 7 | 8 | // IIFE - Immediately-Invoked Function Expression 9 | (async () => { 10 | const person = await getPerson(1); 11 | console.log(person.name); 12 | })(); 13 | -------------------------------------------------------------------------------- /src/sample12.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | const getPerson = async (id) => { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | return await response.json(); 6 | }; 7 | 8 | // IIFE - Immediately-Invoked Function Expression 9 | (async function() { 10 | const person = await getPerson(1); 11 | console.log(person.name); 12 | })(); 13 | -------------------------------------------------------------------------------- /src/sample16.js: -------------------------------------------------------------------------------- 1 | const StarWars = require('./star-wars'); 2 | 3 | async function loadData() { 4 | const sw = new StarWars(); 5 | 6 | const personPromise = sw.getPerson(1); 7 | const filmPromise = sw.getFilm(1); 8 | 9 | const person = await personPromise; 10 | const film = await filmPromise; 11 | 12 | console.log(person.name); 13 | console.log(film.title); 14 | } 15 | 16 | loadData(); 17 | -------------------------------------------------------------------------------- /src/sample11.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | const getPerson = async (id) => { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | return await response.json(); 6 | }; 7 | 8 | const person = await getPerson(1); 9 | console.log(person.name); 10 | 11 | // const person = await getPerson(1); 12 | // ^^^^^ 13 | // 14 | // SyntaxError: await is only valid in async function 15 | -------------------------------------------------------------------------------- /src/star-wars.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | class StarWars { 4 | async getPerson(id) { 5 | const response = await fetch(`http://swapi.co/api/people/${id}`); 6 | return await response.json(); 7 | } 8 | 9 | async getFilm(id) { 10 | const response = await fetch(`http://swapi.co/api/films/${id}`); 11 | return await response.json(); 12 | } 13 | } 14 | 15 | module.exports = StarWars; 16 | -------------------------------------------------------------------------------- /src/sample14.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | // Class 4 | class StarWars { 5 | async getPerson(id) { 6 | const response = await fetch(`http://swapi.co/api/people/${id}`); 7 | return await response.json(); 8 | } 9 | } 10 | 11 | // IIFE - Immediately-Invoked Function Expression 12 | (async () => { 13 | const sw = new StarWars(); 14 | const person = await sw.getPerson(1); 15 | console.log(person.name); 16 | })(); 17 | -------------------------------------------------------------------------------- /src/sample7.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | async function getPerson(id) { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | const body = await response.json(); 6 | 7 | if (response.status !== 200) { 8 | throw Error(body.detail); 9 | } 10 | 11 | return body; 12 | } 13 | 14 | getPerson(0) 15 | .then((person) => console.log(person.name)) 16 | .catch((err) => console.error(err.message)); 17 | 18 | getPerson(1) 19 | .then((person) => console.log(person.name)) 20 | .catch((err) => console.error(err.message)); 21 | -------------------------------------------------------------------------------- /src/sample8.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | async function getPerson(id) { 4 | const response = await fetch(`http://swapi.co/api/people/${id}`); 5 | const body = await response.json(); 6 | 7 | if (response.status !== 200) { 8 | throw Error(body.detail); 9 | } 10 | 11 | return body; 12 | } 13 | 14 | async function loadPerson(id) { 15 | try { 16 | const person = await getPerson(id); 17 | console.log(person.name); 18 | } catch (err) { 19 | console.error(err.message); 20 | } 21 | } 22 | 23 | loadPerson(0); 24 | loadPerson(1); 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "async-await", 3 | "version": "1.0.0", 4 | "description": "async-await samples", 5 | "author": "Roberto Achar ", 6 | "homepage": "https://github.com/robertoachar/async-await#readme", 7 | "keywords": ["node", "async-await", "async", "await"], 8 | "scripts": { 9 | "lint": "eslint src/**/*.js --fix" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/robertoachar/async-await.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/robertoachar/async-await/issues" 17 | }, 18 | "dependencies": { 19 | "node-fetch": "1.7.3" 20 | }, 21 | "devDependencies": { 22 | "eslint": "4.15.0", 23 | "eslint-config-prettier": "2.9.0", 24 | "eslint-plugin-prettier": "2.4.0", 25 | "prettier": "1.10.1" 26 | }, 27 | "license": "MIT" 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Roberto Achar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | --------------------------------------------------------------------------------