Redux Thunk, Sequelize (Bluebird) - 向动作创建者“链”返回承诺

Posted

技术标签:

【中文标题】Redux Thunk, Sequelize (Bluebird) - 向动作创建者“链”返回承诺【英文标题】:Of Redux Thunk, Sequelize (Bluebird) - returning promise to action creator "chain" 【发布时间】:2017-11-14 08:40:28 【问题描述】:

与下面引用的一些以前的帖子类似,我正在尝试通过 Thunk 链接 dispatch,但是我的困难在于从 Sequelize 返回。我可以看到 mysql 查询命中数据库并返回数据,但是该返回没有通过 action-creator 流向后续的.then

我认为这是我尝试使用 Sequelize 的方式 - 我是新手。

非常感谢!

代码:

initDB.js

...
export function sequelizeQry(qryName: string) 
  let query;

  // For later query mapping
  switch (qryName) 
    case 'POSummary':
      query = qry.spPOLedgerCardSummaryALL;
      break;
    default:
      query = qry.spPOLedgerCardSummaryALL;
  

  return new sequelize.Promise(
    () => sequelize.query(query,  type: sequelize.QueryTypes.RAW )
      .then((response) => 
        console.log('Returning promise: ', response);        //<-- This hits the console with data
        return response;
      )
  );

database-actions.js

// @flow
import  fetchingQuery  from '../action-creators/database-creators';

const fetchQuery = (qryName: string) =>
  (dispatch: *) => dispatch(fetchingQuery(qryName));

export default fetchQuery;

database-creators.js

// @flow
// Action Types
import  FETCH_QUERY  from '../constants/ActionTypes';
import  sequelizeQry  from '../utils/initDB';

/** Action-creators */
export function fetchingQuery(qryName: string) 
  const actionType = FETCH_QUERY;

  return (dispatch: *) => 
    dispatch( type: `$actionType_PENDING` );
    sequelizeQry(qryName)                          //<-- This gets called
      .then((payload) => dispatch(                //<-- Don't seem to get here
        type: `$actionType_FULFILLED`,
        payload
      ))
      .catch(err =>
        // Dispatch the error action with error information
        dispatch(
          type: `$actionType_REJECTED`,
          error: err
        )
      );
  ;

我检查过的其他一些参考资料:

    Redux thunk: return promise from dispatched action return promise from store after redux thunk dispatch

【问题讨论】:

sequelize.Promise 是标准承诺吗?如果是这样,您需要将resolve 作为参数传递给promise executor/handler,然后调用resolve(response) 而不是return response 看起来就是这样,非常感谢。 FLOW 一直在唠叨在 .then 中需要 return,我停止思考......非常感谢! 其实return resolve(response) 让FLOW 很开心……我更新了以下内容。再次感谢。 【参考方案1】:

所有功劳归adrice727。

这里是未来访问者的代码更改:

...
  return new sequelize.Promise(
    () => sequelize.query(query,  type: sequelize.QueryTypes.RAW )
      .then((response) => 
        console.log('Returning promise: ', response);        //<-- This hits the console with data
        return response;
      )
  );
...

// BECOMES

return new sequelize.Promise(
    (resolve) => sequelize.query(query,  type: sequelize.QueryTypes.RAW )
      .then((response) => 
        console.log('Returning promise: ', response);
        return resolve(response);
      )
  );

【讨论】:

以上是关于Redux Thunk, Sequelize (Bluebird) - 向动作创建者“链”返回承诺的主要内容,如果未能解决你的问题,请参考以下文章

什么时候应该使用 Redux Saga 而不是 Redux Thunk,什么时候应该使用 Redux Thunk 而不是 Redux Saga?

如何在 Redux/Redux-thunk 中获取 api?

如何在 Typescript 中使用 redux-thunk 使用 ThunkAction 正确键入 thunk?

redux的中间件之redux-thunk

redux的中间件之redux-thunk

redux的中间件之redux-thunk