2016-10-06 9 views
0

私のReact-Nativeアプリでは、ユーザー名とハッシュをサーバーに送信するユーザーログを書き込み、データベースのハッシュとハッシュを比較して結果を返します。私は実装using redux-sagaその:なぜ私のredux-sagaが非同期なのですか

function* fetchUser(action) { 
    try { 
    const user = yield call(Api.fetchUser, action); 
    yield put({type: types.USER_FETCH_SUCCEEDED, user}); 
    } catch (e) { 
    yield put({type: types.USER_FETCH_FAILED, message: e.message}); 
    } 
} 

export function* watchFetchUser() { 
    yield* takeEvery(types.USER_FETCH_REQUESTED, fetchUser); 
} 

export default function* rootSaga() { 
    yield fork(watchFetchUser); 
    // ... 
} 

そして私はconst userは、APIからの応答が含まれていますし、yield put({type: types.USER_FETCH_SUCCEEDED, user})を後に実行することを期待しています。しかし、yield call(Api.fetchUser, action)の後にuserundefinedです。しかしApi.fetchUserは正しい応答を返します。

私は間違いがどこにあるのかわかりません。なぜconst user = yield call(Api.fetchUser, action)の結果がundefinedの場合、Api.fetchUserの後に正しい応答が返されますか?

fetchUser(action) { 
    const url = `${apiUrls.LOGIN_URL}`; 

    fetch(url, 'POST', action.user) 
     .then(response => response.json()) 
     .then(res => res) 
    } 

答えて

1

fetchUser関数から約束を返すことはありません。それはする必要があります

fetchUser(action) { 
    const url = `${apiUrls.LOGIN_URL}`; 

    return fetch(url, 'POST', action.user) 
     .then(response => response.json()) 
     .then(res => res) 
    } 
+0

ありがとうございました! – rel1x

関連する問題