CoffeeScript 如何处理异步调用?

发布于 2024-11-16 16:26:25 字数 244 浏览 4 评论 0原文

当前的异步 JavaScript 调用要求我们使用回调函数。当您需要根据第一次 Ajax 调用返回的数据进行第二次 Ajax 调用时,这可能会导致“兔子洞”代码。

已经有人尝试在不使用回调的情况下进行异步 JavaScript 调用。谷歌,Narative.js。目标是使代码更易于管理和可读。

我的问题是,CoffeeScript 如何处理 Ajax 等异步 JavaScript 调用?是否需要回调或者可以在没有回调的情况下进行异步调用?

Current Asynchronous JavaScript calls require us to use a Callback function. This can lead to "rabbit hole" code when you need to make a 2nd Ajax call based on data returned in a 1st Ajax call.

There have been attempts to make Asynchronous JavaScript calls without the use of Callbacks. Google, Narative.js. The goal being more manageable and readable code.

My question is, How does CoffeeScript handle Asynchronous JavaScript calls like Ajax? Are callbacks required or can Asynchronous calls be made without callbacks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

轻拂→两袖风尘 2024-11-23 16:26:25

CoffeeScript 不提供任何针对异步性的特定功能,因为这必然会导致 CoffeeScript 代码和 JavaScript 输出之间存在很大差距。请参阅有关建议的 defer 语法的讨论:

https://github .com/jashkenas/coffee-script/issues/350

因此,如果您使用 CoffeeScript,您应该使用与 JavaScript 中相同的习惯用法和库来处理异步行为。不同之处在于您的回调将写为 -> ... 而不是 function() {...}

CoffeeScript doesn't offer any particular features geared toward asynchronicity, because this would necessarily cause a large gap between CoffeeScript code and the JavaScript output. See the discussion about the proposed defer syntax:

https://github.com/jashkenas/coffee-script/issues/350

So, if you use CoffeeScript, you should deal with asynchronous behavior using the same idioms and libraries as in JavaScript. The difference is that your callbacks will be written as -> ... rather than function() {...}.

风月客 2024-11-23 16:26:25

Coffeescript 是一种有些人认为比 Javascript 更清晰、更简洁、更容易编写和阅读的语言。 Coffeescript 编译为 Javascript,然后在 Javascript 虚拟机上运行。归根结底,Coffeescript 只能做 Javascript 能够做的事情。

CoffeeScript 如何处理异步调用?

就像 JavaScript 一样。如果您想使用回调,请使用回调。如果您想使用 Narative.js,请使用它。如果你想使用 Jquery,就使用它。

Coffeescript is a language that some people think is clearer, more concise, and easier to write and read than Javascript. Coffeescript compiles to Javascript, which then runs on a Javascript virtual machine. At the end of the day, Coffeescript can only do what Javascript is capable of doing.

How does CoffeeScript handle Asynchronous Calls?

Like Javascript. If you want to use callbacks, use callbacks. If you want to use Narative.js, use that. If you want to use Jquery, use that.

往日 2024-11-23 16:26:25

您可以使用 jquery $.when (不是特定于咖啡脚本的)来使事情更加清晰。

var firstCall = $.get 'stuff.json'
$.when(firstCall).then #make second call

you could use jquery $.when (not coffee script specific) to make things more clear.

var firstCall = $.get 'stuff.json'
$.when(firstCall).then #make second call
顾挽 2024-11-23 16:26:25

您可能想采用https://github.com/mirek/node-flat-flow 使调用链扁平化的方法。它与咖啡脚本配合得很好。

You may want to adopt https://github.com/mirek/node-flat-flow approach to make call chain flat. It works very well with coffeescript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文