Skip to content
youhoc
  • Pages
    • Home
    • Modern App Guidelines
    • Linux
      • Day 1: Linux Distributions & Navigation
      • Day 2: User Management
      • Day 3: File Permission & Ownership
      • Day 4: Package Management
      • Day 5: Services Management
    • Javascript
      • JS The Weird Part
        • Execution Context
        • Types & Operators
        • Objects & Functions
        • Error Handling & Strict Mode
        • Typescript, ES6, Tra
      • Modern JS
        • JS in the Browser
        • Data Storage JSON
        • Modern JS
        • Advanced Objects & Methods
        • Webpack & Babel
        • icon picker
          Async
      • jQuery
        • In-depth Analysis of jQuery
      • React-ready JS
        • Arrow Function
        • Template Literals
        • Logical AND, OR, Ternary, Nullish Operators
        • Destructuring & Rest Operator
        • Array Method
        • Immutability and Spread Operator
        • Promises, Async/Await, Callback
    • PHP
      • gruntJS
      • composer
      • MySQL
    • Docker
      • Container Basics
      • Container Networking
      • Container Image
      • Container Volume & Persistent Data
      • Dockerfile
      • Docker Compose
      • Docker Registry
    • Node.js
      • 1. Installing & Exploring
      • 2. Core Modules
      • 3. Get User Input
      • File System & Input Arguments
      • 5. Express Web Server
      • 6. Deploy to Heroku & Github
      • Authentication
      • 7. Databases
      • 8. Rest API
    • ReactJS
      • React from Andrew
        • Summary from Next
        • 1. Basics
        • 2. React Components
        • 3. Webpack
        • 4. Styling with SCSS
        • 5. React Router
        • 6. React Hook
      • Modern React From The Beginning
        • Intro to JSX
        • Vite Build Tools
        • Basic Component Creation
        • Component State
        • Props & Component Composition
        • useState with Inputs & Form Submission
        • useEffect, useRef & Local Storage
        • Async / Await and Http Request in React
        • React Router: Declarative Mode
        • ContextAPI
        • React Router: Framework Mode
          • File-routing & HTML Layouts
          • Server-side Data Query
          • Links & Navigation
          • Loaders
    • Typescript
      • Type User vs UserProp
    • Payload CMS

Async

Previously we get data from:
self-assign
user-input
local storage

XMLHttpRequest

Use XMLHttpRequest() to get data from HTTP

XML was popular back then, but now we have JSON
XML structure data to transfer, just like JSON
Sync JSON objects in local storage
Read more about XMLHttpRequest

Add Event Listener

listening event: readystatechange
request complete = 4
unsent = 0
request open / initialize = 1
request sent in progress = 2
downloading response = 3
request success = 200
other failed, pending code... (học rồi mà chưa note)

Free open API

Cũng chưa note lại một số open API để thực tập

Callback Abstraction

How do we get return data from Ajax before the page finished loading?
When we make a normal XMLHttpRequest(), it would run synchronously (in top to down order)...
But if we put the XMLHttpRequest() in a function, and return a value, then use that value in the lower code, it would NOT WORK, because JS run asynchronously as default.
The time it takes for the XMLHttpRequest() to return data, would be too slow, during that the lower code would have finished executing...
Even if you return data with the right scope, it would not work...
A CALLBACK FUNCTION IS A FUNCTION WILL BE CALL IN ANOTHER FUNCTION...
For example, we add a function after a click event in addEventListener function...
We will apply this callback function abstraction to solve the asynchronous issue...
BUT, what if we want to do it synchronously with function, how do we do it?
NOTE: DON’T WAIT FOR SOMETHING TO FINISH BEFORE RUNNING OTHER THINGS
YOU DONT KNOW HOW LONG YOU WOULD WAIT

Promise API

callback == promise

resolve == data
reject == error
new Promise == define callback function
myPromise.then() == execute a callback function

A better way to structure async code

Pass params into Promise using Closures


Promises Chaining

Instead of...
too many error duplicates
too deep nested codes
We use promise chanining...

Fetch API

fetch == XMLHttpRequest

but no worry about readystatechange
we don’t have to worry about IF it is completed
we just need to check how it is succeeded (404 or 400 or 200)

Fetch will return a Promise

A Promise that resolve to a return value
which can be used with .then() and catch(error)
but we have to CHAIN promise to convert into object

User Fetch with Chaining and Arrow Function for optimal data fetching

Async / Await

async == Fetch in General Function

Async is used for FUNCTION
Async function always return a Promise..
just like Fetch, but in general concept of a Function
A Promise that resolve to a return value
A Promise that reject to a throw new error

await == .then() chaining in Promise

No more then() multiple times. We can just assign Promise to var using await.
What come after await must be a Promise object
Or in a simple term:
BEST PRACTICE
Tóm tắt:
Screen Shot 2022-02-17 at 1.26.37 PM.png

Đọc thêm từ MDN JS


Async Fetch at Client side
Screen Shot 2022-07-21 at 16.24.47.png
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.