Why do my code lines after await not get called?

前端 未结 2 651
长情又很酷
长情又很酷 2021-01-26 10:41

I have a problem with the following code. The firebase.login returns a Promise and I learned that, when I put \"await\" before, Javascript waits until the Promise delivers and t

2条回答
  •  礼貌的吻别
    2021-01-26 11:09

    The returned Promise from the firebase.login(); call should be wrapped within an async function (which we cannot see from your snippet). As mentioned in the comments it's possible that the catch is being triggered, in which case you should be gracefully handling the error.

    // mock the firebase login Promise
    const firebase = {
      async login(email, password) {
        return {
          email
        };
      }
    };
    
    (async() => {
      try {
        const [email, password] = [
          'joe.bloggs@example.com',
          'password123',
        ];
        const user = await firebase.login(email, password);
    
        console.log(user);
      } catch (err) {
        console.error(err);
      }
    })();

提交回复
热议问题