Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Action Creators: unwrap Promise with Action #215

@alexeyraspopov

Description

@alexeyraspopov

The idea to describe AC as pure functions is good but it has one disadvantage. It looks good only for sync stuff.

function addTodo() {
    return { type: TODO_ADDED, title: "rule the world" };
}

The main purpose of using AC is to isolate asynchronous stuff from other parts of architecture (like view layer and stores layer). And based on my experience on projects with server side (or another types of WebAPI that you can use) you will create a lot of async AC. So it would be better if the developer will be able to write async AC easy just like sync one.

Currently, I need to wrap all my async operations and use dispatch function as a side effect.

function loadProjects(token) {
  return dispatch => {
    WebAPI.projects(token)
        .then(projects => dispatch({ type: PROJECTS_LOADED, data: projects }))
  };
}

Lets accept Promises and unwrap them to get Action instance

function loadProjects(token) {
  return WebAPI.projects(token)
        .then(projects => ({ type: PROJECTS_LOADED, data: projects }));
}

I think it's a way to full consistency and purity.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions