Why does Redux Promise return unresolved promise i

2019-05-02 14:09发布

问题:

I'm having tough time figuring out why this is happening, but essentially Redux Promise was working fine for me while returning something like:

    return {
      type: STORY_ACTIONS.STORY_SPOTIFY_REQUEST,
      payload: request
    }

However, I now need to pass another information with it like so

    return {
     order: 0, // New field
     type: STORY_ACTIONS.STORY_SPOTIFY_REQUEST,
     payload: request
    }

This results in an unresolved promise instead of data. I tried renaming order to something like position or index... still nothing.

回答1:

You should use the meta field, which is required by Redux Promise. Redux Promise uses Flux Standard Actions (FSA), which validates the action with this code:

import isPlainObject from 'lodash.isplainobject';

const validKeys = [
  'type',
  'payload',
  'error',
  'meta'
];

function isValidKey(key) {
  return validKeys.indexOf(key) > -1;
}

export function isFSA(action) {
  return (
    isPlainObject(action) &&
    typeof action.type !== 'undefined' &&
    Object.keys(action).every(isValidKey)
  );
}

export function isError(action) {
  return action.error === true;
}

As you can see, there are only four reserved words for valid keys. So you should add the order property to the 'payload' or maybe 'meta' instead.