简体   繁体   English

如果我在 index.html 的脚本标记中使用异步,我是否需要费心在 scripts.js 中编写异步代码? 我要javascript

[英]If I use async in my script tag in index.html do I need to bother with writing async code in scripts.js? I am asking for javascript

I am new to JavaScript.我是 JavaScript 新手。 I have learned that I can make my scripts async by putting the word async in front in the html script tag.我了解到我可以通过将 async 这个词放在 html 脚本标记的前面来使我的脚本异步。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>    

<script async src="scripts.js"></script>
</body>
</html>

But I saw in another video that I need to use callbacks/ Promises/ Async-await to make my code async.但是我在另一个视频中看到我需要使用回调/承诺/异步等待来使我的代码异步。

 const friendlyFunction = async ()=> {
     return `Hello`
 }

 console.log(friendlyFunction)

Is there any difference?有什么区别吗?

These are two entirely independent concepts.这是两个完全独立的概念。

The async attribute on a script tag means that the script will not be render-blocking - the browser will not wait for the script payload to finish downloading and to finish executing the script before continuing on to render later parts of the HTML.脚本标签上的async属性意味着脚本不会被渲染阻塞 - 浏览器不会等待脚本有效负载完成下载并在继续渲染 HTML 的后面部分之前完成执行脚本。 This would be helpful if you had other stuff below the script tag, eg如果您在脚本标签下方有其他内容,这将很有帮助,例如

<script async src="scripts.js"></script>
<div>more HTML content here</div>

(If you don't have any more content below the script tag, the attribute doesn't really do anything useful) (如果您在 script 标签下没有更多内容,则该属性实际上并没有做任何有用的事情)

In contrast, using the async keyword in front of JavaScript functions will mean相反,在 JavaScript 函数前面使用async关键字将意味着

  • The function will always return a Promise该函数将始终返回一个 Promise
  • If you return a value from inside the function, the returned Promise will resolve to that value如果从函数内部返回一个值,返回的 Promise 将解析为该值
  • You can use await inside the async function to resolve other Promises in a clean, syntactically flat way, without .then您可以在 async 函数中使用await以干净、语法平坦的方式解析其他 Promise,而无需.then

They're quite different uses of the same word, async .它们是同一个词async的完全不同的用法。 You may want to use the async attribute, or you may want to use async functions in some situations, or both, or neither.您可能想要使用 async 属性,或者您可能想要在某些情况下使用异步函数,或者两者都使用,或者两者都不使用。

暂无
暂无

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

相关问题 如果我将脚本放在 html 的底部,我应该在脚本标签上使用异步还是更好 - Should I use async on the script tag or its better if I put the scripts at the bottom of the html 当我从终端运行 gulp 脚本时,为什么我的 gulpfile.js 没有编译为 scripts.js? - Why is my gulpfile.js is not compiling to scripts.js when I run gulp scripts from the terminal? Svelte / Vanilla JS - 我是否需要在等待异步 function 时使用.then()? - Svelte / Vanilla JS - do I need to use .then() on awaiting an async function? Express JS:我还能吗 <scripts> 在我的index.html中 - Express JS: Can I still have <scripts> in my index.html 使用HTML5样板,我想知道如何将我的所有脚本放置在plugins.js和scripts.js文件中? - Using the HTML5 boiler plate I would like to know how I could place all my scripts in the plug-ins.js and scripts.js files? HTML无法识别我要运行的JavaScript文件,SyntaxError:意外令牌&#39;&lt;&#39;(匿名函数)script.js:1 - HTML won't recognize my the javascript file I am asking it to run, SyntaxError: Unexpected token '<' (anonymous function)script.js:1 如何在不触及index.html的情况下将外部javascript标记添加到Ember JS应用程序? - How can I add an external javascript tag to an Ember JS app without touching the index.html? 如果我使用异步/等待,是否需要创建 object 互斥量? - Do I need to make my object mutex if I am using async/await? 如何在 JavaScript 中使用异步/等待? - How do I use async/await in JavaScript? 我可以使用 JS 将异步添加到 iframe 中的脚本标记,以使其实际影响脚本页面加载吗? - Can I use JS to add async to a script tag in an iframe in such a way that it will actually affect the script page loading?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM