14

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

我现在有这个代码

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

this.props.fetchStorage().then(function() {
           console.log('then');
        });

现在说我想更改 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);
            });

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

编辑

只是为了澄清我为什么这样做,我有这样的事情

  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);
                });
        });
    };
}
4

3 回答 3

2

axios是基于promise的,所以你不需要把它包装在Promise中,你可以做这样的事情,还有axiosInstance.[post|put|get| end etc.] 方法将返回承诺。

export function fetchStorage() {
    return function (dispatch) {
       return 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);
                });
    };
}
于 2019-12-27T10:50:20.253 回答
0

必须有更好的方法,但你可以

export async function fetchStorage() {
    return async function (dispatch) {
        try {
            const { data } = await axiosInstant.get('/Storage/Get')
            dispatch({ type: actions.FETCH_STORAGE, payload: { storages: data } })
            return data
        } catch (e) {
            console.error(e)
        }
    }
}

然后你必须使用 fetchStorage 如下

this.props.fetchStorage().then(async function(response) {
    let payload = await response()
});
于 2019-07-04T14:44:38.647 回答
-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);
        });

 });
于 2016-08-19T18:56:03.300 回答