从Axios返回承诺?

cho*_*bo2 12 javascript promise axios

我想知道如何返回Axios的承诺表格?我不确定是否需要使用拦截器?

我现在有这个代码

export function fetchStorage() {
    return function (dispatch) {
      return new Promise(function(resolve, reject) {
        if (1 === 1) {
          resolve('it works!');
        } else {
          reject(':(');
        }
      });
    };
}
Run Code Online (Sandbox Code Playgroud)

this.props.fetchStorage().then(function() {
           console.log('then');
        });
Run Code Online (Sandbox Code Playgroud)

现在说我想改变fetchStorage通过ajax做一些事情,我会喜欢它

 var instance = axios.create({
            baseURL: 'http://localhost:54690/api',
            timeout: 2000,

        });

        instance.get('/Storage/Get')
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            });
Run Code Online (Sandbox Code Playgroud)

我如何回复承诺而不是在这里做?

编辑

只是为了澄清为什么我这样做我有类似的东西

  componentWillMount() {
        this.props.setPreLoader(true);
        this.props.fetchStorage().then(function() {
           this.props.setPreLoader(false);
        });
    }

export function fetchStorage() {
    return function (dispatch) {
        return new Promise(function (resolve, reject) {
            axiosInstant.get('/Storage/Get')
                .then(function (response) {
                    let payload = response.data;
                    console.log(payload);
                    dispatch({ type: actions.FETCH_STORAGE, payload: { storages: payload } });
                })
                .catch(function (error) {
                    console.log(error);
                });
        });
    };
}
Run Code Online (Sandbox Code Playgroud)

小智 -6

参见示例:

 return new Promise(function (resolve, reject) {
    var instance = axios.create({
        baseURL: 'http://localhost:54690/api',
        timeout: 2000,
    });
    instance.get('/Storage/Get')
        .then(function (response) {
            if(1 === 1)
            resolve(response);
        })
        .catch(function (error) {
            reject(error);
        });

 });
Run Code Online (Sandbox Code Playgroud)