Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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
Javascript react redux异步操作完成函数后调用下一个函数_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript react redux异步操作完成函数后调用下一个函数

Javascript react redux异步操作完成函数后调用下一个函数,javascript,reactjs,redux,Javascript,Reactjs,Redux,假设我有一个函数调用api: export default function AuthApi({url, method, headers, data}={}){ return (dispatch, getState) => { fetch(url, { method: method || null, headers: headers || null, body: form || null,

假设我有一个函数调用api:

export default function AuthApi({url, method, headers, data}={}){
    return (dispatch, getState) => {

        fetch(url, {
            method: method || null,
            headers: headers || null,
            body: form || null,
            }).then(function(response) {
                return response.json();
            }).then(function(response){
                console.log(response)
            })

    }
}
现在我想在某个地方调用此api:

      dispatch(AuthApi({url: "some_url", method: "POST", data: data}))
      console.log("called api")
      dispatch(userInfo(response))
      console.log(getState())
      router.pushState(null, '/profile')
这里我使用
dispatch
调用api,然后调用
dispatch(userInfo)
。 我假设我的
dispatch(userInfo())
函数在
dispatch(AuthApi())

但在这里,它进入了
AuthApi()
,但没有完成它,它就开始调用其他函数或进程

dispatch(AuthApi())
完全完成后,我怎么能只调用我的其他函数或逻辑,或者调用
console.log()


谢谢您

您可以使用承诺,它们与
Thunk中间件
配合得非常好:

dispatch(AuthApi({url: "some_url", method: "POST", data: data})).then((response) => {
    dispatch(userInfo(response))
})

更新 您还应该修改操作以返回承诺

export default function AuthApi({url, method, headers, data}={}){
    return (dispatch, getState) => {    
        return fetch(url, {
            method: method || null,
            headers: headers || null,
            body: form || null,
        }).then(function(response) {
            return response.json();
        })    
    }
}

尝试将
return
添加到操作中