15

In closure tag wiki page, it reads "jQuery itself is one big closure."

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. Maybe because there are other features of promise like $.Deferred(), resolve(), and fail() to expand its functionality beyond a simple function passing?

Community
  • 1
  • 1
Forethinker
  • 3,548
  • 4
  • 27
  • 48
  • 1
    I see a closure a litle differently ([From the Wikipedia](http://en.wikipedia.org/wiki/Closure_(computer_science))). BTW nice question (+1). – Geeky Guy Aug 27 '13 at 17:27
  • @Renan: Maybe this should be another question, but if there is a referencing environment and it can be changed depending on how the function is called, can I simplify and say "it is used with different environments?" From my understanding, this corresponds to "the function can access variables not in its parameter list". But I may be using the term 'environment' loosely here due to my ignorance. Could you please point to me to reference for the definition of function environment? Or if I am missing the whole point here, could you describe briefly why there is a difference from your POV? – Forethinker Aug 27 '13 at 17:40
  • Check out my answer and the quotes will take you to great bits on each subject. – ars265 Aug 27 '13 at 17:44
  • 1
    I feel like this might be more appropriate for [Programmers.SE]. – zzzzBov Aug 27 '13 at 17:45
  • @zzzzBov You're probably right, but it's good for everyone to know some of the basics for a good understanding – ars265 Aug 27 '13 at 17:46
  • @Forethinker it should be another question, indeed, and I think if you look for it, you'll find it answered already in Stack Overflow. – Geeky Guy Aug 27 '13 at 17:48

4 Answers4

17

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. there are other SO questions that describe this very well

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.

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. 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

whereas jQuery's implementation uses at least one closure, but isn't really based on them

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. In the end it is neither impossible or necessary to implement it like that.

Community
  • 1
  • 1
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
  • Thank you for the straight-forward answer. – Forethinker Aug 27 '13 at 17:58
  • This leaves me with the question I came here with: how can closures used in Promises? When I use Promises, sometimes I want values to be sent implicitly to then-functions from various previous Promises. See the end of https://stackoverflow.com/questions/28250680/how-do-i-access-previous-promise-results-in-a-then-chain/57681704#57681704 for my solution using closures explicitly created using 'bind'. – David Spector Aug 27 '19 at 20:21
6

You wouldn't ask if a birdhouse was a 2x4, even if you used one to make it. The same can be said of promises and closures. Promises make use of closures to retain references to state, callbacks and other such things.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
5

Because of the nature of JavaScript, being asynchronous that is, we are provide much power by the language and it's runtimes. 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. 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. This means that not only executing a function but doing so in possibly an entirely different context.

ars265
  • 1,949
  • 3
  • 21
  • 37
  • Promises are also not unique to jQuery. http://wiki.commonjs.org/wiki/Promises/A – Ben McCormick Aug 27 '13 at 17:47
  • No, they aren't but he mentioned jQuery by name, so that is how I replied. The main point is the definitions of what they are which I have tried to lay out. I did add that to my answer to help that point. – ars265 Aug 27 '13 at 17:48
2

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.

Borrey
  • 63
  • 7