Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React/Redux和JavaScript Promise.all_Javascript_Reactjs_Promise - Fatal编程技术网

React/Redux和JavaScript Promise.all

React/Redux和JavaScript Promise.all,javascript,reactjs,promise,Javascript,Reactjs,Promise,我对诺言有意见,一切都在反应。我正在为我的axios请求创建承诺,并在最后运行一个Promise.all来分派所有数据 以下是我创建承诺的方法: function promiseLoad(nameID, uID, caption) { return new Promise( () => { axios.get(`${URL}/users/${uID}/name/${nameID}`) .then(res => {

我对诺言有意见,一切都在反应。我正在为我的axios请求创建承诺,并在最后运行一个Promise.all来分派所有数据

以下是我创建承诺的方法:

function promiseLoad(nameID, uID, caption) {
    return new Promise( () => {
        axios.get(`${URL}/users/${uID}/name/${nameID}`)
            .then(res => {
                let obj;
                if(res.data !== -1) {
                    obj = {
                        fullName: res.data,
                        caption: caption
                    };
                }
                return obj;
        });
    });
}
然后,在我的导出方法中,在将其发送到减速机之前运行Promise.all

export const loadUsers = (users, uID) => {
    return dispatch => {
        let promises = [];
        imgs.forEach(element => {
            promises.push(promiseLoad(element.id, uID, element.caption));
        });
            console.log('Promises');
            console.log(promises);
        Promise.all(promises)
            .then(res => {
                console.log('here');
                console.log(res);
                dispatch(getUsers(res));
        });
    }
}
getUsers只是返回类型/操作的助手方法

export const getUsers = (res) => {
    return {
        type: actionTypes.GET_USERS,
        payload: res
    }
}
运行代码时,我可以在日志中看到:

Promises
Array(10) // array with 10 pending promises

Promise.all的
.then()
方法中的日志永远不会运行。

axios.get
已返回一个Promise,因此不需要将其包装在Promise构造函数中。请注意,如果您确实构建了一个承诺,为了避免它永远无法解析,您至少必须在executor中调用
resolve
。在您的案例中,
promiseLoad
返回了一个从未解决的承诺,因此您没有看到这些日志

函数promiseLoad(nameID、uID、标题){
返回axios.get(`${URL}/users/${uID}/name/${nameID}`)
。然后(res=>{
让obj;
如果(分辨率数据!=-1){
obj={
全名:res.data,
标题:标题
};
}
返回obj;
});
}