last day (18 days later) » 

2:56 PM
1
A: While loop with A+ promises in JavaScript / node.js

TomalakThe following solution addresses a few specific problems: how to break the (otherwise endless) loop how to access the result of a previously failed attempt how to incorporate your params = modify(params) The problem can be broken down into two sections: a repeater/promise runner (that its...

 
@BenjaminGruenbaum I disagree. My code looks like that, it isn't that. (Okay, apiReques() could be made marginally simpler, but the rest?)
 
1) Unfortunately your last version in that Gist isn't equivalent anymore to my original idea. 2) I don't understand how Promise.resolve() as a way to create a new promise is better than new Promise(). It's fewer lines, but is it better? (I do understand that Promise.resolve(p) is nicer, though.) 3) I don't subscribe to the idea that rejected promises are like exceptions. If a promise encapsulates a process that can fail naturally (like setting a value and getting back a "not allowed") then I would expect that promise to reject, not resolve with a falsey value.
Maybe I'm wrong with my last point. Assuming I talk to an HTTP API in order to set the value foo to 5. That request can fail on multiple levels. It can fail physically (timeout), it can fail on server (404 or 500), it can fail in the application (access denied). I would expect a rejected promise in every one of these cases, because the promise was to set the value foo to five, and it didn't work. Resolving the promise with "false" for the "access denied" case does not feel right.
 
The last version is not equivalent but the one just before is. Promise.resolve() just gives you an empty promise - you don't have to create your own completion source and your code is not concerned with all the wiring of rejections and errors so it's a lot easier to not get wrong - your apiRequest function can just be function apiRequest(){ request(params).then(function(res){ if(!isGood(res)) throw res; return res; });}. In HTTP APIs I believe that a rejection is for a timeout or an application fail is good but I would not reject on a 404 error myself at the HTTP request layer.
 
2:56 PM
Well, when properly abstracted, setFoo(5).then(joy).catch(noJoy) wouldn't even expose the fact that HTTP is involved. When the server replies "no" that's not an HTTP error, but but it still means noJoy for me. Right?
 
Are you talking about an RPC failure here or a protocol failure? When you make an HTTP request the promise wrapper for that should reject if the request itself failed - not if the request was successful and the server returned a response we didn't expect. When wrapping that API in an RPC API you can check if the server did not do what we expected it to and throw which would give you that behavior. Anyway - if you want to talk more about this I'm often in the JS room in chat. It kind of got offtopic here.
Hey
 
@BenjaminGruenbaum Why use the crowded general JS chatroom when we can have a specific one right here?
;-)
 
Well, there are several other discussions in the JS room that are pretty interesting about this question and dbs and I'd have to control+tab to switch but sure :)
What's up?
 
I think if there wasn't an exception (i.e. something broke) then code shouldn't throw (because throwing causes stack unwinding and that's slow)
and "access denied" is a normal response, not an exception.
 
@Tomalak promises are actually throw safe.
 
3:00 PM
that's not the point ;-)
 
If you throw in a promise you get a rejected promise - not an unhandled async exception.
Throwing isn't slower and/or faster than rejecting.
 
Of course is throwing slower, because that triggers the call stack to unwind
 
Every time you have a promise handler there is a try catch around it and rejections are created from exceptions. Also - modern browsers generate the stack trace lazily and errors in JS contain the stack at the time of their creation on them (not when they're thrown) so it's creating the error object that's expensive.
(At least V8 JSC and SM do this, Chakra is closed source so I couldn't check)
 
hm
 
Like you said though - the expense isn't the point here - a promise rejection signals something went wrong in the operation.
 
3:02 PM
Okay, I didn't see it that way until now.
 
There are actually really good papers by Mark Miller and Barbara Liskov about how promises can be used in RPC and what to do in scenarios (reject/fulfill etc)
In fact the paper that pioneered promises in the 80s discusses this exactly (RPC, not rejections, rejections are more of a JS thing - a lot of languages don't have rejections or don't treat them as special).
Like you said though - an RPC failing is likely a rejection and not just a false value - the HTTP on the other hand succeeded. The RPC call should definitely fail.
Speaking of RFC you might want to look at Kris's q-connection it does that sort of thing - I wonder how he dealt with it.
 
My point.
 
Yeah, we never disagreed on that - but the RPC can do:
return http.get(...).then(function(response){
    if(!response.successful) throw new RPCError(...);
    return response.data;
})
 
correct
well, wait
 
Anyway - my issue with the answer was just the deferred anti pattern - the promise constructor is meant to be used when converting APIs mainly.
 
3:07 PM
So every time I use new Promise() that's automatically an antipattern?
 
Not automatically, sometimes you have to convert callback APIs to promises and have to use it.
 
I consider this more clear and readable than Promise.resolve().then(...)
 
It's for creating new promise sources, mainly. Also, if you're creating new forms of aggregations it can be fine.
@Tomalak in libraries like bluebird, it's Promise.try(... instead of Promise.resolve().then(...
 
I guess I'll have to think about this some more. I'm ambivalent, but I tend to say you're right.
I'm specifically unsure about the "using a falsey value when resolving a promise to indicate failure" part you have in your Gist. That would prevent returning false as a valid resonse.
 
@Tomalak well, the gist isn't for HTTP responses directly - it's meant to simulate a for loop.
A for loop terminates (in every language but Python) when the condition gets a false value.
 
3:14 PM
that's because Python only has a "for each", not a "for".
 
It's because Python raises (read throws) a StopIteration in order to stop a loop - that's its iteration protocol.
 
Yeah, it's the exact same issue I mentioned above about throw- I don't think it's right to use exceptions as a means of controlling flow
I always thought of that point as awkward in Python as well, but obviously it's a matter of preference
 
Yes, I think it's awkward in Python which is why I added a version that doesn't use it for flow control in the gist :)
 
I'll think about this some more and will make the appropriate changes to my answer. Clearly I'm not yet as proficient with promises as I'd like to be. Thanks for your time. :-)
 
Sure thing :) Thanks for the discussion
 

  last day (18 days later) »