简体   繁体   English

REDUX THUNK ASYNC AWAIT返回Promise中间件

[英]REDUX THUNK ASYNC AWAIT to return a Promise middleware

I have a problem, i am using redux thunk as my API middleware, and it works fine when it just updates the redux state. 我有一个问题,我正在使用redux thunk作为我的API中间件,并且当它只是更新redux状态时,它可以正常工作。 However there are parts of my codes that require me to get an response from the request API rather than waiting for it to go through the redux store.. How can i "Promisify" this part ? 但是,我的代码中有些部分要求我从请求API中获取响应,而不是等待它通过redux存储。。我如何“承诺”这部分?

my code for calling the api with axios: 我用axios调用api的代码:

export async function callApi(apiRoutes, method, params) {
  method = method.toUpperCase();
  let apiArr = apiRoutes.split('/')
  let newApiRoute = await Promise.all(apiArr.map(async (queryBreakDown, index) => {
    let value
    if (queryBreakDown.indexOf(':') > -1) {
      queryBreakDown = queryBreakDown.replace(':', '');
      if (!params) throw Error('No Params');
      value = params[`${queryBreakDown}`];
      delete params[`${queryBreakDown}`];
      return value;
    } else {
      value = queryBreakDown
    }
    return value
  }))
  newApiRoute = await newApiRoute.join('/')

  // NOTE: add pagination queries
  if (params && params.count) {
    newApiRoute += '?count=' + params.count
    delete params.count
    if (params.page) {
      newApiRoute += '&page=' + params.page
      delete params.page
    }
    //NOTE: add filters to query
    if (params.filter) {
      for (var property in params.filter) {
        newApiRoute += `&filter[${property}]=` + params.filter[`${property}`]
      }
      delete params.filter
    }
  }

  // NOTE: add file upload queries
  if (params && params.fileType && !params.count) {
    newApiRoute += '?fileType=' + params.fileType
    delete params.fileType
  }

  let data = params ? params : null
  // cookie.set('user_token', '123123123');
  // cookie.remove('user_token', { path: '' });
  let accessToken = cookie.get('user_token');
  let headers = {};
  if (accessToken) {
    headers['Authorization'] = `Bearer ${accessToken}`;
  }
    headers['Content-Type'] =  'application/json';
  const response = await axios({
    method: method,
    baseURL: host + '/api',
    url: newApiRoute,
    headers,
    data
  });

  return response
}

my redux thunk api middleware: 我的redux thunk api中间件:

export default store => next => (action) => {

  const apiRoutes = action[CALL_API];


  const { method, types, params } = action;

  next({ type: types[0] });

  return callApi(apiRoutes, method, params).then(
    response => next({
      response,
      type: types[1],
    }), error => next({
      error: (error && error.message) || 'Something bad happened',
      type: types[2]
    })
  )
}

the function that i need to get a return value immediately from, where getS3UploadUrl is dispatching getS3UploadUrl to the api middleware: 我需要立即从中获取返回值的函数,其中getS3UploadUrl将getS3UploadUrl调度到api中间件:

  let signedURL = getS3UploadUrl({
      fileType: compressedImage.type
    })

probably i need an return value for signedURL as the API request's response 可能我需要一个signedURL返回值作为API请求的响应

However, signedURL is undefined. 但是,signedURL是未定义的。

function mapDispatchToProps (dispatch) {
  return {
    getS3UploadUrl: function(params) {
      return dispatch(getS3UploadUrl(params))
    }
  }
}

i did not keyed in the RETURN for the dispatch OMG .. face palm. 我没有为调度OMG ..脸部掌控输入RETURN。

Also, i notice that it would be better if my callApi function would be synchronous instead of async 另外,我注意到,如果我的callApi函数是同步的而不是异步的,那会更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM