为什么等待后我的代码行不被调用? [英] Why do my code lines after await not get called?

查看:57
本文介绍了为什么等待后我的代码行不被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码有疑问. firebase.login返回一个Promise,我了解到,当我在前面输入"await"时,Javascript将一直等到Promise交付,然后继续下一行.I

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 then continues with the next line.I

但是似乎永远不会触发下一行.我究竟做错了什么?它也不会在调试器"标记处停止.

But the next line(s) seem never to be triggered. What am I doing wrong? It also does not stop at the "debugger" mark.

    try {
      const user = await firebase.login(email, password);
      console.log("l1: ", user);
      debugger;
      props.history.replace("/impressum");
    } catch (error) {
      alert(error.message);
    }
  }```

推荐答案

解决方案:

我从新英雄那里得到了解决方案:

I got the solution from my new hero:

提交按钮提交表单.

提交表单后,浏览器将导航到新的网页.

When a form is submitted, the browser navigates to a new web page.

由于JS程序所在的页面已被导航到远离它的位置,因此该JS程序将退出.它在到达console.log("l2");"

Since the page the JS program was running in has been navigated away from, that JS program exits. It does this before reaching console.log("l2");"

因此,我要做的就是插入"event.preventDefault".在本教程中,使用了一个简单的Button,它不会导致表单提交.我使用了引导提交按钮,这就是为什么我认为表单总是提交的原因.

So, everthing I had to do was insert "event.preventDefault". In the tutorial the used a simple Button, that does not cause the form to submit. I used a bootstrap submit-button und thats why I think the form was always submitted.

这篇关于为什么等待后我的代码行不被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆