Redux saga:yield put在嵌套回调中不起作用

时间:2018-01-29 17:56:52

标签: ecmascript-6 redux firebase-authentication generator redux-saga

const { payload: {loginType, email, password, notification, self} } = action;

  console.log("--TRY--");
  Firebase.login(loginType, { email, password })
    .catch(function(result) {
      const message =
        result && result.message ? result.message : 'Sorry Some error occurs';
      notification('error', message);
      self.setState({
        confirmLoading: false
      });
      isError = true;
    })
    .then(function(result) {
      if (isError) {
        return;
      }
      if (!result || result.message) {
        const message =
          result && result.message
            ? result.message
            : 'Sorry Some error occurs';
        notification('error', message);
        self.setState({
          confirmLoading: false
        });
      } else {
        self.setState({
          visible: false,
          confirmLoading: false
        });
        console.log("--RIGHT BEFORE I CHECK AUTH STATE--");

        //the following does NOT fire
        firebaseAuth().onAuthStateChanged(function*(user) {
              console.log("THE GENERATOR RUNS");
              if (user) {
                  console.log(user);
                  yield put({
                      type: actions.LOGIN_SUCCESS,
                      token: 'secret token',
                      profile: 'Profile'
                  });
                  yield put(push('/dashboard'));
              }
              else {
                  yield put({ type: actions.LOGIN_ERROR });
              }
          });
      }
  }); });

您好。我目前正在第一次使用redux saga。我一直试图在firebaseAuth()。onAuthStateChanged监听器的回调中激活产量。 yield关键字在不是ES6生成器的函数中不起作用,所以我在回调中添加了一个星号,但现在它根本不会执行。非常感谢有关此事的任何建议。

1 个答案:

答案 0 :(得分:2)

正如您所注意到的,redux-saga效果只能在生成器函数中使用,并且您不能将生成器函数用作常规函数:调用生成器函数只返回一个特殊对象。

正确的方法是使用$(document).ready(function(){ // validate the comment form when it is submitted // $("#gen_form").validate(); // validate signup form on keyup and submit $("#gen_form").validate({ rules: { 'fname' : { letters: true, maxlength: 10, minlength:2 }, 'lname' : { letters: true, maxlength: 10, minlength:2 }, 'email': { email: true }, }, messages: { fname: { maxlength: "Your last name must not consist more than 40 characters", minlength: "Your last name must consist of at least 2 characters" }, lname: { maxlength: "Your last name must not consist more than 40 characters", minlength: "Your last name must consist of at least 2 characters" }, email: "Please enter a valid email address" } }); }); :它可以将您的传奇链接到redux生态系统外部的事件源。

首先使用提供的工厂函数创建eventChannel:它会向您发送可用于发出事件的eventChannel函数;然后使用emit效果消耗这些事件。

take