简体   繁体   English

Javascript。 在前端安全使用 async 和 await 吗?

[英]Javascript. Is safe use async and await on front end?

I'm building a web application using angular.我正在使用 angular 构建一个 Web 应用程序。 I know that with ES6 i can use the async/wait, i found this guide how to use them transforming an Observable to Promise.我知道在 ES6 中我可以使用 async/wait,我找到了这个指南如何使用它们将 Observable 转换为 Promise。 https://labs.encoded.io/2016/12/08/asyncawait-with-angular/ But it is safe? https://labs.encoded.io/2016/12/08/asyncawait-with-angular/但它安全吗? the browser is single thread, and i'm little bit afraid to use it thinking that it block all the UI.浏览器是单线程的,我有点害怕使用它,因为它会阻止所有的 UI。 Can someone give more information about it?有人可以提供更多有关它的信息吗? Thanks谢谢

It's not going to block the thread;它不会阻塞线程; at least not more than writing callback-based code .至少不会超过编写基于回调的代码 That's essentially it's whole point;这基本上就是重点; It allows writing asynchronous code as-if it was synchronous;它允许编写异步代码,就好像它是同步的一样; making the flow far easier to reason about.使流程更容易推理。

It is more or less syntactic sugar over on top of generators and promises , which are non-blocking mechanisms for managing asynchronous actions.它或多或少是在 generators 和 promises 之上的语法糖,它们是用于管理异步操作的非阻塞机制。

That being said, they're not magic;话虽如此,它们不是魔法。 the same mistakes that can make callback-based code block the thread can also block when written in async / await .同样的错误会使基于回调的代码阻塞线程也可以在async / await编写时阻塞。

Async/await is experimental and part of ES7 . Async/await 是实验性的,是 ES7 的一部分 Server-side, NodeJS supports it (latest versions) but in a browser, it will almost certainly not be supported.服务器端,NodeJS 支持它(最新版本),但在浏览器中,它几乎肯定不会被支持。 Use good old Promises/Ajax instead.改用旧的 Promises/Ajax。

the browser is single thread, and i'm little bit afraid to use it thinking that it block all the UI浏览器是单线程的,我有点害怕使用它,因为它会阻止所有的 UI

Async/await / Promises / Ajax won't block the UI, by definition, because they are asynchronous.根据定义,Async/await/Promises/Ajax 不会阻塞 UI,因为它们是异步的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM