getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
179.16k stars 33.47k forks source link

Your cheese burger metaphor for promise API is... lacking some points. #726

Closed imanghafoori1 closed 8 years ago

imanghafoori1 commented 8 years ago

Hi, Kyle I thought very much about the cheese burger metaphor but ... let me mention something about it.

The whole point of a promise is to "Let You Go" and asynchronously do your other tasks while they process your request for in this example "cheese burger".

But you are still assuming that you have to wait for the food in the restaurant, still "BLOCK"ed, with the receipt.( I mean you are not free to leave there to do your other tasks.)

reasoning and thinking about the cheese burger in your mind is still possible even you do not have any promise.(Having the receipt is not enabling you think about your cheese burger.)

I do know you do not get angry when someone complains at you and even eager to hear from your students.;)

I have another metaphor for the promises which I think describes the promises better. 1- Imagine you want to build a small factory. (building a factory === writing an application)

2- you want to consume electricity in your factory and produce heat. (electricity === input data) (heat === output data)

3- you do not have the electricity power subscription yet. You go and request government to give you electricity.(a subscription) And they give you a promise to let you have electricity power in next the week. The give you a powerless wire now and say: "Electricity will come along this wire in the next week. We can not connect you to the network right now. " (Request government to give you electricity === Sending Ajax request to server) (The powerless wire === a promise in the pending state)

4- You do not wait blocked for a week, you continue and start to setup your small factory utilities. You setup all the internal wires and utilities within the your private building and determine if in the future the electricity comes into your factory, what should happen to it. (whether gets converted into light or heat or ...) even thought all the wire are still empty of power. (Setup all the internal wires and utilities === writing the ".then(fn...)" stuff ) (factory wires and utilities === the callback function passed to .then(...))

5- In the next week you suddenly you see that electricity comes and all the lights get illuminated and etc.(your utilities start to produce heat.) (Electricity comes in === promise gets resolved with a value) (utilities start to produce heat === fn callback function gets executed)

The only downside of this metaphor is that it describes the streams (observables) rather than the single valued promises. because the stream nature of electricity power.

imanghafoori1 commented 8 years ago

Technically, the promise values are always pushed TO consumer BY producer,(like electricity come to you along the wire, by itself) but the food does is not pushed to you. You should got and get it...

it was better to say that after ordering the food for tomorrow, you leave there to do your other tasks. And they send you a cheese burger to your address.(deliver you the food.)

imanghafoori1 commented 8 years ago

How the factory metaphor describes The "inversion of control" issue? You do not give your utility of your factory to guys of power plant in order to power them up for you only because they are producing electricity.(If you give them your assets you should trust them a lot.)

you just ask them for power, and keep all your assets private within your factory so you are in control of how they should work together as a unit.

getify commented 8 years ago

My goal is not to have one metaphor that describes all aspects of promises. Indeed, I present several different metaphors (future cheeseburger, event listener, callback manager) that each describe different aspects of how to think about promises.

The future cheeseburger metaphor is only about the future value aspect -- a value that's not present yet but will be. It has very little to do with the async flow control part of promises.

imanghafoori1 commented 8 years ago

The teaching tricks... I do not know. Maybe it is good for your students to also know a metaphor that describes every thing, maybe it is not.

I just wanted to share my ideas to help. Hope fully.

Any way the cheeseburger was helpful for me in the past. در تاریخ ۹ مهٔ ۲۰۱۶ ۵:۳۱ قبل‌ازظهر، "Kyle Simpson" notifications@github.com نوشت:

My goal is not to have one metaphor that describes all aspects of promises. Indeed, I present several different metaphors (future cheeseburger, event listener, callback manager) that each describe different aspects of how to think about promises.

The future cheeseburger metaphor is only about the future value aspect -- a value that's not present yet but will be. It has very little to do with the async flow control part of promises.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/getify/You-Dont-Know-JS/issues/726#issuecomment-217757095

getify commented 8 years ago

Thanks for the suggestions. Will consider in future revisions. :)

imanghafoori1 commented 8 years ago

You are welcome dear teacher, I was able to write my own open source library on the top of GSAP only
after I watched your frontendmasters.com - Advanced javascript workshop github.com/imanghafoori1/pretty-gsap

On Mon, 09 May 2016 17:25:39 +0430, Kyle Simpson
notifications@github.com wrote:

Thanks for the suggestions. Will consider in future revisions. :)

— You are receiving this because you modified the open/close state. Reply to this email directly or view it on GitHub

Using Opera's mail client: http://www.opera.com/mail/