使用Redux获取帖子的作者和承诺的顺序?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (33)

使用React Native和Redux,我试图首先获取一些帖子,然后获取相应的作者。

我面临两个问题:

  1. 如何修复我catchthen声明的顺序?我想说:如果帖子不能牵强,调度失败的行动,停止,不要做别的(即千万不能尝试获取作者)。但是如果帖子成功获取,请继续获取作者。
  2. 我需要在下面的代码中更改以确保posts传递给我fetchAuthorsForPostsFromAPI?由于是,postsundefinedfetchAuthorsForPostsFromAPI
// fetches the posts from the API
export const fetchPostsFromAPI = () => ( dispatch ) => {

    // dispatch action: currently fetching posts from the API
    dispatch( fetchingPosts() );

    // prepare the promise that fetches the posts from the API
    let loadPosts = new Promise( ( resolve, reject ) => {
        // uses dummy data for now
        if( !postsInitial ) {
            reject( "Error: Could not get posts" );
        }
        else {
            resolve( postsInitial );
        }
    });

    // actually fetch the posts from the API
    return loadPosts
        // if error fetching the posts from the API
        .catch( error => dispatch( fetchPostsFailure( error ) ) )
        // if success fetching the posts from the API
        .then( posts => dispatch( fetchPostsSuccess( posts ) ) )
        // fetch authors for posts from the API
        .then( posts => fetchAuthorsForPostsFromAPI( posts, dispatch ) )
}
// is dispatched if the posts were succesfully fetched from the API
function fetchPostsSuccess( posts ) {
    return {
        type: PostConstants.FETCH_POSTS_SUCCESS,
        postsWithoutAuthors: posts,
    }
}
// is called when the posts have been fetched successfully from the API
// fetches the author for each post from the API
function fetchAuthorsForPostsFromAPI( posts, dispatch ) {

    console.log( "posts:", posts ); // logs "posts: undefined"

    // dispatch action: currently fetching authors of posts from the API
    dispatch( fetchingAuthorsForPosts() );

    // prepare the promise that fetches the authors for the posts from the API
    let loadAuthors = new Promise( ( resolve, reject ) => {

        // for each post
        posts.map( post => {

            // find the author by id
            const authorObj = authors.find( author => {
                return author.id === post.authorId
            });

            // if error finding author
            if( !authorObj ) {
                reject( "Error: Author for this id could not be found!" );
            }

            // if success finding author, keep going
            else {
                post.author = authorObj;
            }

        });

        // resolve
        resolve( posts );
    });

    // actually fetch the authors from the API
    return loadAuthors
        // if success fetching the authors for the posts from the API
        .then( posts => dispatch( fetchPostsAuthorsSuccess( posts ) ) )
        // if error fetching the authors for the posts from the API
        .catch( error => dispatch( fetchPostsAuthorsFailure( error ) ) );
}
提问于
用户回答回答于

有几种方法可以解决这个问题。使用async / await的Imho比承诺容易得多。

    async const fetchData = () => (dispatch) =>  {
      var postResult = await getPosts(dispatch);
      if (postResult) {
        await getAuthors();
      }
    }

    const getPosts = async () => (dispatch) =>  {
      var apiResponse = await getPostsFromApi();
      if(apiResponse)
      {
        //dispatch something
        return true;
      } else {
        //dispatch error
        return false;
      }
    }

扫码关注云+社区

领取腾讯云代金券