Giter Club home page Giter Club logo

redux-async's Introduction

Redux Asynchronous Simple Example

Installation

  git clone https://github.com/HamSungJun/Redux-Async.git

  In Project root Directory/

    $ npm install
    $ npm run dev -> localhost:9000

Description

redux-thunk 모듈을 통해 상태관리 상황에서의 비동기 요청에 대처.

Demo

demogif

Why do we need synchronous loading process?

서버로부터의 HTTP request를 요청하는 상황에서 자바스크립트 코드는 비동기적으로 동작한다. 만약 서버로부터의 온전한 응답을 받기전에 렌더링이 시작된다면 아무런 데이터도 표시해 줄 수 없게 된다. 이를 해결하기 위해서는 다음과 같은 방법들이 존재한다.

  1. setTimeout()메소드를 통해 일정시간 기다린후 렌더링.
(Not Recommended)

네트워크 상황은 예측할 수 없기때문에 일정 시간 기다리는 것은 퍼포먼스 하락을 야기하고 기다리는 일정시간 이후에도 응답이 올 수 있음.

  1. redux-thunk 미들웨어와 fetch().then().catch() 메서드를 이용.

서버측의 응답을 온전히 받음을 보장받고 다음의 프로세스를 진행함.

[./Actions.js]

export const AC_FETCH_MOVIE_DATA_START = () =>{

  // 해당함수는 redux-thunk 미들웨어를 지나는중에 실행된다.
  return (dispatch,getState) => {

    // Fetching State를 변경하여 로딩화면을 렌더링.
    dispatch({

      type : A_FETCH_MOVIE_DATA_START,
      isFetching : true

    })

    // 서버쪽의 API를 호출.
    fetch("https://yts.am/api/v2/list_movies.json")
    .then(response => response.json())
    .then((response)=>{

      // 서버쪽의 응답을 온전하게 받고 JSON 오브젝트로 파싱완료된 상황.
      dispatch({
        type : A_FETCH_MOVIE_DATA_DONE,
        response : response.data.movies,
        isFetching : false
      })

    })
    .catch((error)=>{
      // 오류 상황 발생시 오류 메시지를 렌더링.
      dispatch({
        type : A_FETCH_MOVIE_DATA_ERROR,
        isFetching : false,
        error : error
      })

    })

  }

}

redux-async's People

Contributors

hamsungjun avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.