简体   繁体   English

承诺关闭?

[英]Is promise a closure?

In closure tag wiki page , it reads "jQuery itself is one big closure." 闭包标签维基页面中 ,它显示“jQuery本身就是一个大关闭”。

But is promise a closure as well? 但是也承诺关闭? Could you please explain why or why not? 你能解释一下为什么或为什么不解释? This is how I understand closure: assign a function to a variable and reuse it with different environments. 这就是我理解闭包的方法:为变量分配一个函数并在不同的环境中重用它。 Promise does that with $.ajax() , but I could not find anywhere in stackoverflow where promise is introduced as a closure. Promise使用$.ajax()来做,但我无法在stackoverflow中找到将promise作为闭包引入的地方。 Maybe because there are other features of promise like $.Deferred() , resolve() , and fail() to expand its functionality beyond a simple function passing? 也许是因为还有其他的承诺功能,如$.Deferred()resolve()fail()以扩展其功能,超越简单的函数传递?

Closures 关闭

This is how I understand closure: assign a function to a variable and reuse it with different environments. 这就是我理解闭包的方法:为变量分配一个函数并在不同的环境中重用它。

That's not a strictly accurate definition of a closure. 这不是一个严格准确的闭包定义。

A closure is a function that has access to a referencing-environment. 闭包是一种可以访问引用环境的函数。 In Javascript, that means a function that is returned by another function and has access to the original functions scope. 在Javascript中,这意味着由另一个函数返回的函数,并且可以访问原始函数作用域。 there are other SO questions that describe this very well 还有其他SO问题很好地描述了这一点

Closure's are general purpose structures that can be used in a variety of ways. 闭包是可以以各种方式使用的通用结构。 One of their biggest benefits is that they protect private scope, which is why libraries like jQuery are often written as closures, so that they don't need to expose all their functions globally. 它们最大的好处之一是它们保护私有范围,这就是为什么像jQuery这样的库通常被编写为闭包,因此它们不需要在全局公开它们的所有功能。

Promises 承诺

Promises are a different concept. 承诺是一个不同的概念。 They are a way of structuring asynchronous code to make it easier to follow the flow. 它们是构造异步代码的一种方式,可以更容易地遵循流程。 A promise object in particular is an object that provides functions to chain operations in a clear and easy to read way. 特别是promise对象是一个以清晰易读的方式为链操作提供函数的对象。 A promise might be implemented using closures, but it does not have to be. 可以使用闭包实现承诺,但不一定如此。 For instance here is an implementation that does not use closures: 例如,这里是一个不使用闭包的实现:

https://gist.github.com/814052/690a6b41dc8445479676b347f1ed49f4fd0b1637 https://gist.github.com/814052/690a6b41dc8445479676b347f1ed49f4fd0b1637

whereas jQuery's implementation uses at least one closure, but isn't really based on them 而jQuery的实现使用至少一个闭包,但实际上不是基于它们

http://james.padolsey.com/jquery/#v=1.10.2&fn=jQuery.Deferred http://james.padolsey.com/jquery/#v=1.10.2&fn=jQuery.Deferred

Conclusion 结论

Promises and Closures aren't directly related concepts. 承诺和闭包不是直接相关的概念。 Closure's are a programming technique that might be used in a Promise implementation. Closure是一种可以在Promise实现中使用的编程技术。 In the end it is neither impossible or necessary to implement it like that. 最终,既不是不可能也不是必须这样实现它。

You wouldn't ask if a birdhouse was a 2x4, even if you used one to make it. 你不会问一个鸟舍是否是2x4,即使你用它来做它。 The same can be said of promises and closures. 承诺和封闭也是如此。 Promises make use of closures to retain references to state, callbacks and other such things. Promise使用闭包来保留对state,callbacks和其他类似东西的引用。

Because of the nature of JavaScript, being asynchronous that is, we are provide much power by the language and it's runtimes. 由于JavaScript的本质,即异步,我们通过语言和运行时提供了很多功能。 First off, a Promise in jQuery, although it is not unique to jQuery, is an object that will as the documentation puts it, observe when all actions of a certain type bound to the collection, queued or not, have finished . 首先,jQuery中的Promise虽然不是jQuery独有的,但它是一个对象,它将随着文档的说明, observe when all actions of a certain type bound to the collection, queued or not, have finished This means you can use this object to know when to continue after a set or queue of items has finished some behavior. 这意味着您可以使用此对象知道在一组或一组项目完成某些行为后何时继续。 Now a Closure on the other hand is not unique to jQuery, but is rather a JavaScript construct, one that combines two things: a function, and the environment in which that function was created . 现在, Closure并不是jQuery独有的,而是一个JavaScript构造,它combines two things: a function, and the environment in which that function was created This means that not only executing a function but doing so in possibly an entirely different context. 这意味着不仅执行一个函数,而且可能在完全不同的上下文中执行。

Closures and Promise are different concepts. 闭包和承诺是不同的概念。 Closures refers to scope of variables where as promise are used to 'promise' that an act on something will occur when it is done on an asynchronous action. 闭包是指变量的范围,其中承诺用于“承诺”在异步操作完成时对某事件的行为将发生。 Since Javascript is non-blocking (not asynchronous --edit), it will not wait for a function to get a response if it needs to access the internet or disk, that said you can have a promise execute after something is done. 由于Javascript是非阻塞的(不是异步的 - 编辑),它不会等待函数获得响应,如果它需要访问互联网或磁盘,它表示你可以在完成任务后执行一个承诺。

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

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