如何处理CoffeeScript的异步调用? [英] How does CoffeeScript handle Asynchronous Calls?

查看:208
本文介绍了如何处理CoffeeScript的异步调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前异步JavaScript调用需要我们使用一个回调函数。当您需要根据在第一次Ajax调用返回的数据第2个Ajax调用这可能会导致兔子洞code。

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.

有过尝试,使无需使用回调异步JavaScript调用。谷歌,Narative.js。我们的目标是更易于管理和易读code。

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.

我的问题是,如何处理CoffeeScript的异步JavaScript调用Ajax的一样?正在回调需要或异步调用在不回调做?

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

推荐答案

CoffeeScript中并没有提供对异步性面向任何特定的功能,因为这会造成一定CoffeeScript的code和JavaScript输出之间有很大的差距。请参阅有关的讨论所提出的延迟语法:

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

所以,如果你使用的CoffeeScript,你应该处理使用相同的成语和库在JavaScript中异步行为。所不同的是你的回调将写为 - > ... ,而不是函数(){...}

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() {...}.

这篇关于如何处理CoffeeScript的异步调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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