Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

102. 递归 ajax 请求获得后端分页数据 #102

Closed
ly525 opened this issue Jul 12, 2018 · 1 comment
Closed

102. 递归 ajax 请求获得后端分页数据 #102

ly525 opened this issue Jul 12, 2018 · 1 comment

Comments

@ly525
Copy link
Owner

ly525 commented Jul 12, 2018

const http = {
  get() {
   async fetch_table_list(url='/table_list_api/', payload={}){
      return await axios.get(url, payload).then(res => {
        // next 为请求下一页数据的完整URL,由后端框架封装,直接请求即可
        if (!res.body.next) {
          return res.body.results
        }

        return http.get.fetch_table_list(res.body.next).then(lastResults => {
          return [].concat(res.body.results, lastResults)
        });
      });
    } // fetch_table_api end
  } // get end
}
// page
this.$store.dispatch('FetchHouseList');


// vuex
// 实现方式 1
actions: {
  FetchHouseList({ commit, state, dispatch }, payload) {
    return api.fetchHouseList(payload).then(res => {
      /* next page url */
      if (!res.nextPage) return res.results;
      return dispatch('FetchHouseList', { url: res.nextPage }).then(prev => {
        return [].concat(res.results, prev);
      });
    });
  } // FetchHouseList end
}

// 实现方式2
actions: {
  FetchHouseList({ commit, state, dispatch }, payload) {
    return new Promise((resolve, reject) => {
      api.fetchHouseList(payload).then(res => {
        /* next page url */
        if (!res.nextPage) {
          resolve(res.results);
          return
        }
        dispatch('FetchHouseList', { url: res.nextPage }).then(prev => {
          resolve([].concat(res.results, prev));
        });
      });
    });
  } // FetchHouseList end
}

相关链接:

  1. js的promise如何递归调用?
  2. 119. add retry for python requests #119 add retry for python requests
@ly525
Copy link
Owner Author

ly525 commented Jul 20, 2018

#119

@ly525 ly525 added the HTTP label Dec 7, 2018
Repository owner locked and limited conversation to collaborators Jan 23, 2024
@ly525 ly525 converted this issue into discussion #644 Jan 23, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

1 participant