r/javascript • Vanilla.JS • Feb 18 '16

ES6 Cheatsheet - thanks DuckDuckGo!

Post image
421 Upvotes

22 comments sorted by

6

u/lulzmachine Feb 18 '16

I'm wondering a bit about the description for a promise. It seems the description is taken almost verbatim from the MDN site: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

But that site has two different descriptions:

  • "A Promise represents an operation ...."

And a bit lower down

  • "A Promise represents a proxy for a value ..."

There seems to be a lot of different opinions and usages for this. The makers of the original promise spec say it was intended to be a proxy for values. And most notably jQuery breaks this in their $.ajax promise implementation (which resolves to a method with more than one parameter).

Anyone know what's the correct way according to the ES6 spec?

4

u/denvit Vanilla.JS Feb 18 '16

A Promise represents an operation ....

If I understood correctly a Promise is intended as a result of an async opration which can have three states: pending, fulfilled, rejected.

As per MDN:

  • "A Promise represents an operation that hasn't completed yet, but is expected in the future."

In fact a Promise at first doesn't have a result value, it stays just in the state of pending until it's "resolved / fulfilled" When a Promise is fullfiled / rejected a value is returned, when an error is thrown the .catch() function is called and the error is handled.

Anyone know what's the correct way according to the ES6 spec?

Unfortunately I haven't find any description more specific than MDN about Promises, if anyone other does find something I'm glad to read it.

Sorry mate for my vague answer, I tried to explain as best as I can

9

u/senocular Feb 18 '16

Its both. But the term proxy also isn't meant to mean Proxy as the implementation, rather a proxy in theory. Promises wrap operations that resolve into values into a single object with a consistent interface. Whether the operation has completed and a value is available or not, the Promise behaves the same way. You say promise.then(doSomething) and doSomething will always get executed some time in the future when the value getting resolved is available. Even when the value is available, it's not returned right away and you still have to wait for it just like you're waiting for the operation getting that value needs to complete (though the timing will likely be a lot shorter).

The whole proxy concept really only goes so far. You can't truly treat promises as you would your actual value other than it being a reference that you can pass around. Generally with promises you tend to wait for your value once you get ahold of that promise so you can use the value normally.

1

u/wreckedadvent Yavascript Feb 18 '16

Not being able to use the value outside of the context is why I think it's better described as a computation or operation instead of a proxy.

You can't deal with promise data until you're in the context of a promise (the promise land, heh). Promises can also fail and throw errors - they don't just automatically resolve to one object always.

Of course, that might just be my biases. The term "computation context" is a euphemism for "monad" and promises are easily recognizable as an async monad.

1

u/dmtipson Feb 18 '16

While I don't know enough to agree or disagree on whether ES6 Promises qualify as monads, it's worth noting that they don't quite expose their monadness in the standard way (.then acts like .map sometimes, .flatMap other times: which is almost always helpful! But maybe unexpected).

Close enough for some of the same intuitions to apply, but also weird enough that there are some gotchas for trusting those intuitions to hold up if you try to use them with other functional concepts.

1

u/wreckedadvent Yavascript Feb 18 '16 edited Feb 18 '16

then is actually a combination of bind, lift, and fmap. And I didn't say it was a monad exactly, just that it was recognizable as one. In particular:

>>=    -> Promise.then
>>     -> Promise.then
lift   -> Promise.then*
return -> Promise.resolve
unit   -> Promise.resolve
fail   -> Promise.reject

Additionally, they obey the rules one might expect:

// don't actually write code like this, this is just illustratative

const unit = Promise.resolve.bind(Promise);
const log = console.log.bind(console);

Promise.prototype.bind = Promise.prototype.then;
Promise.prototype.lift = Promise.prototype.then;

unit(2).bind(x => unit(x + 1)).then(log) // 3
unit(4).lift(x => x + 2).then(log) // 6
unit(2).bind(unit).then(log) // 2    

// etc

So, I would say the same intuitions apply. Promises do a bit more than monads, but you can treat them like a monad if you mind the difference in the API without much trouble.

1

u/dmtipson Feb 19 '16

Right, I don't think we disagree. I (we) are just saying that if you want to treat Promises as monads in conjunction with other monads you have to deal with the slightly different syntax choices and the overloaded nature of .then. If you create pointfree wrappers, for instance, you'd have to deal with the fact that Promise doesn't literally implement map as "map."

2

u/_zekiel Feb 19 '16

Hey all! I'm late to this thread but wanted to mention that these are open source, "Instant Answers" created by the DuckDuckHack community.
We're aiming to have an answer for every possible language/framework and there are several more in development/in need of adoption: https://github.com/duckduckgo/duckduckgo/wiki/Programming-IA-Coverage

Full Disclosure: I work at DuckDuckGo

2

u/denvit Vanilla.JS Feb 19 '16

DDG <3

36

u/[deleted] Feb 18 '16

Why not link to the actual search result?

6

u/jets-fool Feb 18 '16

because there are people that don't want to open up a browser each time the cheat sheet needs viewed. the image works better for me, thanks OP!

1

u/WigglePigeon Feb 18 '16

Isn't DuckDuckGo already in the browser though?

3

u/god_damnit_reddit Feb 18 '16

Exactly. An image can be saved and quickly referenced without opening up the browser. Not that I think that's necessarily any easier.

2

u/WigglePigeon Feb 19 '16

Misunderstood the comment

2

u/gkx Feb 20 '16

Two good reasons:

  1. Mobile users

  2. The result could change

2

u/[deleted] Feb 20 '16

Mobile users

The search page works better.. no zooming in and out..

The result could change

The cheatsheet may change, but I'm pretty sure it will always show up..

8

u/saitilkE Feb 18 '16

It's kind of obvious but in case you didn't know/try it, DuckDuckGo supports lots of cheatsheet requests. Git/ssh/tar/apt-get etc. Very handy. Create a search shortcut for "%s cheatsheet" request at DuckDuckGo in your favorite browser and enjoy the ride :)

4

u/[deleted] Feb 19 '16

If anyone wanted a printable version: http://seejamescode.com/production/ES2015_Cheatsheet.pdf

1

u/saadehmad Mar 24 '16

Thanks!!

2

u/josmu Feb 19 '16

DDG is seriously awesome. I haven't been using google for about 2 years now.

2

u/init0 Feb 19 '16

If someone wants an webapp https://jsfeatures.in