|
ES6 finalized the modules spec a while back, and various transpilers like Traceur let you use the syntax ahead of direct browser support. But yeah, browsers will implement ES6.
|
# ? Jul 23, 2015 20:05 |
|
|
# ? May 18, 2024 05:43 |
|
Thermopyle posted:What's the plan on module support in the browser? How's that going to work? This is the eventual syntax (sorry if that's not what you were asking) : http://www.2ality.com/2014/09/es6-modules-final.html e: oh you wanted browser implementation details, um, uh, erm... Kekekela fucked around with this message at 10:39 on Jul 24, 2015 |
# ? Jul 23, 2015 20:19 |
|
I just can't wrap my head around promises. Does anyone have a fool proof tutorial/explanation?
|
# ? Jul 23, 2015 20:30 |
|
Knifegrab posted:I just can't wrap my head around promises. Does anyone have a fool proof tutorial/explanation? I've been reading through these books - https://github.com/getify/You-Dont-Know-JS Here's the book & chapter that discusses Promises: https://github.com/getify/You-Dont-Know-JS/blob/master/async%20&%20performance/ch3.md Edit: Hopefully that helps. I'm probably in the same boat as you. I won't see more benefits until I see more real world examples. geeves fucked around with this message at 20:40 on Jul 23, 2015 |
# ? Jul 23, 2015 20:37 |
|
Knifegrab posted:I just can't wrap my head around promises. Does anyone have a fool proof tutorial/explanation? I found this blog post to be great as it covers some of the common misuses of Promises and does a good job of illustrating the pitfalls. After reading the blog I began following Nolan Lawson on twitter and it is both cool and good. http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html
|
# ? Jul 23, 2015 20:38 |
|
Dr. Poz posted:I found this blog post to be great as it covers some of the common misuses of Promises and does a good job of illustrating the pitfalls. After reading the blog I began following Nolan Lawson on twitter and it is both cool and good. quote:Q: What is the difference between these four promises? Hah, this is a pretty tricksy question but I douby anybody in the world could fit the right answer into a reply tweet.
|
# ? Jul 23, 2015 21:29 |
|
Subjunctive posted:ES6 finalized the modules spec a while back, and various transpilers like Traceur let you use the syntax ahead of direct browser support. But yeah, browsers will implement ES6. Yeah, I've been using babel with browserify and the import/export syntax for months now. My real question is what this will look like when the browser implements it. I guess it just fires off a request each time it hits an import statement so we don't have to do the current bundling step?
|
# ? Jul 23, 2015 23:00 |
|
Thermopyle posted:Yeah, I've been using babel with browserify and the import/export syntax for months now. My real question is what this will look like when the browser implements it. I guess it just fires off a request each time it hits an import statement so we don't have to do the current bundling step? I think any improvements will depend on browser and server HTTP 2 support, which if I understand it correctly, allows to stream multiple files per request.
|
# ? Jul 24, 2015 02:25 |
|
spacebard posted:I think any improvements will depend on browser and server HTTP 2 support, which if I understand it correctly, allows to stream multiple files per request. HTTP 2 can't fix this by itself, because it's still lovely to wait for file X to download before requesting file Y. If anything, the build step will be MORE complicated because the web server itself will need to understand the dependency graph in order to push it, instead of just the JS bundler.
|
# ? Jul 24, 2015 05:43 |
|
There's been talk and perhaps a spec floating around to bundle some sort of giant requirement list in native HTML5/ES6 like this though.
|
# ? Jul 24, 2015 05:50 |
|
Dr. Poz posted:I found this blog post to be great as it covers some of the common misuses of Promises and does a good job of illustrating the pitfalls. After reading the blog I began following Nolan Lawson on twitter and it is both cool and good. Thank you, reading through this blog post now, hopefully it helps! geeves posted:I've been reading through these books - https://github.com/getify/You-Dont-Know-JS I read through a few different parts of the different books to see how I felt about this author and I like it so much I just ordered every book he had off amazon on paperback. Thanks guys, I am a pretty amateur JS developer, but I am working for the big boys now and my chops definitely need to improve!
|
# ? Jul 28, 2015 18:30 |
|
What's a good resource for teaching people async programming? I've tried explaining it a dozen times over the past few weeks to my underlings but it's just not sticking and I feel like I'm gonna go insane.
|
# ? Jul 29, 2015 03:05 |
|
obstipator posted:What's a good resource for teaching people async programming? I've tried explaining it a dozen times over the past few weeks to my underlings but it's just not sticking and I feel like I'm gonna go insane. I'm too tired to work this into a coherent example but gather them all in the kitchen and send them one at a time to fetch things. Then send them all at once for things. Then send some people and give others instructions that rely on the objects being retrieved by the currently running threads/people. also try thinking about them as humans beings rather than lings
|
# ? Jul 29, 2015 03:34 |
|
That works for getting the gist of race conditions and stuff for the truly uninitiated, but to me the largest hurdle for people (esp. in a js context) isn't understanding that concept but rather having the sensitivity to notice when something is async or not. To me that is what trips people up far more than getting how a callback or promise works. They can follow a contrived example and get it, but in practice they are just misidentifying many non-blocking call as blocking. Typically that boils down to just learning to be vigilant to the specifics of what a function call is going to do, and breaking it down to either i/o or in-memory-only (a coarse but generally applicable guideline for determining if something is blocking or not). As for suggestions, if they get the fundamentals, maybe scour up some code snippets with some that correctly catch async behavior and some that do not (stackexchange should be rife with both) , and have them quiz themselves on being able to tell whether snippet B is correctly written or not, if not where is the race condition, how to fix, etc...
|
# ? Jul 29, 2015 20:02 |
|
Like... what is an example of a non-blocking call that someone who can program computers misidentified as blocking?
|
# ? Jul 29, 2015 20:07 |
|
Most recently I've seen it with file i/o and gulp, specifically. Like doing some stream/pipe/dest chain and then assuming the output file will be there and ready for usage downstream in the main execution line of the gulp task or wherever. On a local build you may not notice the problem because you aren't minifying and taking extra steps that a production build would, so the files ends up being ready in time just by chance, and/or a version of it already existed on your local so you don't get errors. Maybe what I mean is more that if you're a developer who isn't seasoned to think about async consequences all the time, and you're dealing with an async call that manipulates state in one area*, then it makes for an easy trap to forget/overlook that when trying to access the same piece of state in another area. * Or worse, have those calls wrapped such that no callback or .then is required to use the wrapper, probably due to another unseasonsed developer who thought they were being tidy and helpful by "containing" the async call
|
# ? Jul 29, 2015 21:32 |
|
Bhaal posted:That works for getting the gist of race conditions and stuff for the truly uninitiated, but to me the largest hurdle for people (esp. in a js context) isn't understanding that concept but rather having the sensitivity to notice when something is async or not. To me that is what trips people up far more than getting how a callback or promise works. They can follow a contrived example and get it, but in practice they are just misidentifying many non-blocking call as blocking. Typically that boils down to just learning to be vigilant to the specifics of what a function call is going to do, and breaking it down to either i/o or in-memory-only (a coarse but generally applicable guideline for determining if something is blocking or not). Ohhhhhh, that makes a lot of sense that they understand the concept but can't easily acknowledge which functions are async without spending extra time looking it back over. Thanks. Makes me feel better since I thought my explanations on async were going partially ignored, and since I'm working with nodejs--async at its finest..--there's many more opportunities to misinterpret non-blocking as blocking.
|
# ? Jul 30, 2015 04:02 |
|
obstipator posted:What's a good resource for teaching people async programming? I've tried explaining it a dozen times over the past few weeks to my underlings but it's just not sticking and I feel like I'm gonna go insane. Gazpacho fucked around with this message at 00:04 on Aug 3, 2015 |
# ? Aug 3, 2015 00:00 |
|
I hate promises and callbacks. I just cannot get my dumb head around them, I feel hopeless and lost, and I've read almost every resource I can find on it. I have a simple problem, I am writing in node, I have a postgres database. I want to use pg package to return the results of a query. I want to do this async using promises. But I honestly don't even know where to begin or how to do it. It seems like the pg's query function is blocking, so can I just not use promises with it? I honestly am so confused. My brain is so bad at async.
|
# ? Aug 8, 2015 00:43 |
|
Why do you believe that pg's query function is blocking? Do you have a small code sample that you believe demonstrates that? e: As someone who gets by at writing asynchronous JS when absolutely necessary, I will confirm that it's an exceptional PITA, and I would never tell someone that they suck just because they've missed some fine points of scope and callback-threading. Gazpacho fucked around with this message at 03:42 on Aug 8, 2015 |
# ? Aug 8, 2015 02:39 |
|
Knifegrab posted:I hate promises and callbacks. I just cannot get my dumb head around them, I feel hopeless and lost, and I've read almost every resource I can find on it. If you suck at async code, you either suck at modeling the flow of the problem you're trying to solve, or you suck at doing something with the state you get back. Once you stop leaking state all over the place, it just becomes a way of decomposing your flow into different chains that execute at the same time. It might help if you flowchart the path of remote data through your program. If nothing else, it can help us write some boilerplate code for you to start with. Think of a promise like a box that might have a value inside. When you do something (.then()) on the resolution of the promise, you're basically saying:
There's nothing crazy or complicated about that, the promise chain is just some code sequence sitting around until someone dumps a value in the box.
|
# ? Aug 8, 2015 02:45 |
|
disclaimer: I'm a novice with promises but I work in some pretty promise heavy code-bases so it was vital to my sanity that I comprehend them somewhatKnifegrab posted:It seems like the pg's query function is blocking, so can I just not use promises with it? When you say its blocking, do you mean from a logical standpoint the program can't continue until the query is complete? If so then then I think the "You're Missing the Point of Promises" section here will probably be useful. I know you said you've read all the resources, but this may be extremely on point I think. It really helped me reframe promises in my head as "callback aggregator thinger..." to providing the correspondence between synch and async functions he describes in that gist: Domenic Denicola posted:The thing is, promises are not about callback aggregation. That's a simple utility. Promises are about something much deeper, namely providing a direct correspondence between synchronous functions and asynchronous functions.
|
# ? Aug 8, 2015 11:47 |
|
Knifegrab posted:I hate promises and callbacks. I just cannot get my dumb head around them, I feel hopeless and lost, and I've read almost every resource I can find on it. If you don't want to think about it, use pg-promise and there you go. EDIT: https://github.com/vitaly-t/pg-promise/wiki/Learn-by-Example might be helpful as well. EDIT the 2nd: There's another super simple library: https://github.com/coderhaoxin/pg-then/blob/master/index.js that you can read through the source to see how they do it, and maybe that will help you wrap your head around promises a bit. Lumpy fucked around with this message at 20:52 on Aug 8, 2015 |
# ? Aug 8, 2015 20:46 |
|
Speaking of promises and async modeling in general, how do I handle a situation where I want to report aggregate statistics? For example, say I have an array of URLs and I use Node's request module to grab them. Aside from global state, how do I accumulate e.g. the total size or the total number of links or whatever? It's not clear how to "wait" for a bunch of async calls to complete and do something with all the results, or to incrementally build up a result as the async calls complete.
|
# ? Aug 10, 2015 03:30 |
|
Kobayashi posted:Speaking of promises and async modeling in general, how do I handle a situation where I want to report aggregate statistics? For example, say I have an array of URLs and I use Node's request module to grab them. Aside from global state, how do I accumulate e.g. the total size or the total number of links or whatever? It's not clear how to "wait" for a bunch of async calls to complete and do something with all the results, or to incrementally build up a result as the async calls complete. Use Promise.all([...]) to get a new promise that resolves when all of your promises resolve, then just wait on that promise as you normally would with a list of your final values.
|
# ? Aug 10, 2015 03:42 |
|
piratepilates posted:Use Promise.all([...]) to get a new promise that resolves when all of your promises resolve, then just wait on that promise as you normally would with a list of your final values. Hmm, OK, that was the nudge I needed. It seems like promises are an all or nothing thing -- either the whole code should use them or nothing will ever make sense.
|
# ? Aug 10, 2015 04:28 |
|
Kobayashi posted:Hmm, OK, that was the nudge I needed. It seems like promises are an all or nothing thing -- either the whole code should use them or nothing will ever make sense. You can still use standard async control with promises (after all, a promise is just an object you attach a callback to on completion), it's just that the promises-way tends to work out just as good or better than not using them.
|
# ? Aug 10, 2015 04:56 |
|
Kekekela posted:disclaimer: I'm a novice with promises but I work in some pretty promise heavy code-bases so it was vital to my sanity that I comprehend them somewhat OK I will try to make up some assumptions about promises and the code I want to write, I will admit I am probably wrong. - The standard PG library is blocking because the request function does not contain a callback in its paramaters, so since its not utilizing callbacks, its a sync function not async. - Promises only work when used with async functions already. - However I also am under the impression that promises are a good way to keep a system from being bogged down by blocking requests (such as a slow SQL request). I literally want to take a single file of code, connect to my database (a util file I already have) and just do one request to a table in postgres like this: "select * from config.chapters". But I want to do it in promises without using any extra extensions. I guess I am just confused as to how to wrap something in a promise or how to start a promise off. I just hate promises.
|
# ? Aug 10, 2015 18:21 |
|
Knifegrab posted:I guess I am just confused as to how to wrap something in a promise or how to start a promise off. I just hate promises. code:
code:
Vulture Culture fucked around with this message at 19:02 on Aug 10, 2015 |
# ? Aug 10, 2015 19:00 |
|
Vulture Culture posted:If you need to interact with a library which does not natively support promises, then most promise implementations like when.js or Bluebird or whatever contain a lift() function which takes as a parameter a (possibly Node-style) function and wraps that function to return a promise instead. So instead of Would it be possible to get this example but within the q library which is the library I am working in?
|
# ? Aug 10, 2015 19:07 |
|
Knifegrab posted:OK I will try to make up some assumptions about promises and the code I want to write, I will admit I am probably wrong. quote:- Promises only work when used with async functions already. The intention of a promise is to take your function, run it, and box up two things about it: (1) when it finishes and (2) what it returns. Then the promise object is attached to that metaphorical box and returned to the caller. The caller will then call things like .then which will attach further events of what to do with the your function's return value (and that's where the async part gets put in). When your function is ready (ie. that "box" has the when & what components of your function satisfied) the function in .then will be triggered to run. The fact that your a+b function will be ready immediately (because it already ran) is insignificant to the promise object. It still goes through all the motions and it just so happens that as soon as .then is ready it gets executed right away. Now, when a function you call returns the actual value you're looking for, a promise can wrap it directly like with a+b above. However when your function requires a callback of its own, in order to behave the same way, the promise object needs to attach its own callback (a deferred object, which has its own internal promise which is what gets returned in the main thread of execution). From the promise object's perspective, it is saying "Look, your function doesn't directly return the value you're looking for and it does not signify that it is completed. Instead it is the _callback_ that gets passed to your function that we're interested in, so let's give it my own callback and I'll now use that as my trigger for when you function is both finished and has a return value that we're interested in". This of course ends up being the most typical use case because wrapping a+b is generally pretty silly (but it can be done, and might be a helpful exercise in demystifying what promises are actually doing). quote:- However I also am under the impression that promises are a good way to keep a system from being bogged down by blocking requests (such as a slow SQL request). quote:I literally want to take a single file of code, connect to my database (a util file I already have) and just do one request to a table in postgres like this: "select * from config.chapters". But I want to do it in promises without using any extra extensions. code:
(EDIT: left some var declarations out, coffeescript is great but leads to bad habits) Bhaal fucked around with this message at 20:00 on Aug 10, 2015 |
# ? Aug 10, 2015 19:49 |
|
Bhaal posted:I'm not sure which library you're referring to. Do you have a link to it? I looked at a few node / pg libs and they all seemed to take a callback arg. If the one you're looking at doesn't, then it might have promises baked in and what it's returning is a promise, and is therefore non-blocking. Thank you so much, this helps a lot. I just looekd up teh PG library I am using, it is here: https://github.com/brianc/node-postgres/wiki/Client I am assuming since there are no references to promises being returned, that htis library does not return a promise upon executing the query method. However I do now see that the query method does accept an optional callback. So would you second example of code be most appropriate? Secondly, if a function or method does not return a promise, or take a callback in its parameters, how would I tie it into a promise? Finally in your example, where: code:
|
# ? Aug 10, 2015 20:01 |
|
Knifegrab posted:Finally in your example, where: Yes, "rows" is the return value of the query method. Also, .then can have one or two functions. The first one is the success function, and the second one (if it exists) is the error-handling function. It's optional, kind of like how functions don't have to handle exceptions, and can trust their callers to handle them. Knifegrab posted:Would it be possible to get this example but within the q library which is the library I am working in? Just use Q(...). code:
bartkusa fucked around with this message at 20:57 on Aug 10, 2015 |
# ? Aug 10, 2015 20:50 |
|
bartkusa posted:Yes, "rows" is the return value of the query method. "rows" in this case can be called anything correct? So the value of rows is determined as what would normally be the result var in callback(result,[err]) that makeNodeResolver takes over correct?
|
# ? Aug 10, 2015 21:21 |
|
Knifegrab posted:"rows" in this case can be called anything correct? So the value of rows is determined as what would normally be the result var in callback(result,[err]) that makeNodeResolver takes over correct? Yes, "rows" is just the name of your argument for that function. It can be "iDontGetThisShit" if you want. Also yes to your second question.
|
# ? Aug 10, 2015 21:34 |
|
necrotic posted:Yes, "rows" is just the name of your argument for that function. It can be "iDontGetThisShit" if you want. Also yes to your second question. iDontGetThisShit is a good variable name for all of my variables as they relate to Promises. But thanks for the help guys, I was able to get a query up and running utlizing promises in Q. Thanks for all your help, I do sort of get it now!
|
# ? Aug 10, 2015 21:43 |
|
Honestly they take a 70/30 mixture of practice and reading to really have it all sort of click. Most of my "ah-ha" moments came from discovering & tracking down race conditions that snuck up on me after I thought I had done everything correctly. But to get there you have to build something big enough with all the little app-specific complexities and so on that your code ends up being more than just a handful example snippets cribbed from the internet. But that way you at least will have some substantial code to tinker and troubleshoot with.
|
# ? Aug 10, 2015 22:29 |
|
Proud to say I got my first file up and running utilizing promises! Thanks for everyones help, I am really starting to understand it now. I do have one more question. When I am chaining my promises together, my promises look something like this: function1() .then(function2) .then(function3) .done(); For example. However what if I wanted to pass function 2 or function 3 parameters? How would I do that? Right now I am just utilizing global parameters, which works, but I feel like maybe its not the best thing to do?
|
# ? Aug 11, 2015 19:16 |
|
Knifegrab posted:Proud to say I got my first file up and running utilizing promises! Thanks for everyones help, I am really starting to understand it now. I do have one more question. Make a function that returns a function that will be used as a callback in a promise: code:
|
# ? Aug 11, 2015 20:00 |
|
|
# ? May 18, 2024 05:43 |
|
There's a few other (simpler) ways you can do this. Using bind(): code:
code:
|
# ? Aug 11, 2015 20:16 |