5 ms·
I'm pretty new at promises, but doesn't any appearance of a "promise of a promise" indicate a trivial mis-use of promises? Isn't the whole point of promises th
by recursive 10y ago
I'm pretty new at promises, but doesn't any appearance of a "promise of a promise" indicate a trivial mis-use of promises? Isn't the whole point of promises that they can be chained together without nesting?
- pandler 10y agoThat was my thought as well. A promise that returns a promise that returns a value is no different than a promise that returns a value.
- supster 10y agoexactly, I'm not sure why you would be having promises of promises
- phpnode 10y ago> Isn't the whole point of promises that they can be chained together without nesting? Yes, it's an excedingly common misunderstanding and probably one of the main causes of people being "turned off" by promises. It's kind of frustrating to see so many blog posts making this mistake when critiquing them.
- vog 10y agoIndeed. And if you generalize this idea only slightly, you get ...... monads! Maybe promises will be the vehicle that makes understanding the usefulness of monads available for the masses. Alternatively, promises may fail the acceptance test for the same reason as monads - because the average developer simply doesn't care / doesn't get it. I really hope for the first scenario, though.
- pekk 10y agoIf you are not trying to use Haskell (which has monads as a workaround for certain problems) then what is the point having monads? The average developer doesn't care because they are working in an environment where monads aren't needed. Trying to drag monads into arbitrary languages like Javascript isn't some kind of best practice, it's cargo culting.
- deleted 10y ago[deleted]
- andrewflnr 10y agoA Monad is a general-purpose mathematical structure, not just a workaround. GP gave you an example of something they're useful for, which you completely ignored.
- berdario 10y agoWhat's the point of having monads? You might as well ask what's the point of having Iterable<> when you're programming in C :) It's not that you strictly _need_ monads or Iterable interfaces, it's that they represent an abstract concept which allows you to solve problems in a generic and type-safe way I wouldn't probably "drag monads" into Javascript, but I'd surely appreciate Functors when programming in Javascript :)
- adrusi 10y agoHaskell doesn't have monads as a work-around. They are a useful abstraction because they capture a common semantic pattern found in many programming concepts. Nullable types, error handling, sequential execution, and asynchronous event handling are all concepts that can be described monadically. By describing them as monads and making the monadic pattern a core idea of the language, you can write algorithms that operate on any monad, so that you can write one function and have it be useful for both managing the execution of IO and error handling in a generic way. People have an aversion to monads because a the concept of a monad has no grounds in reality, kind of like complex numbers. It is an abstraction that is useful not because it describes anything, but because it is simply a useful way to think.
- vog 10y ago> If you are not trying to use Haskell (...) then what is the point having monads? I'm not sure how to answer this question, as my previous comment actually is the answer to your question. Promises are a great example. They are a mechanism that provides better encapsulation and is still easier to compose than using pure callbacks. (IIRC, Haskell made a similar transition: Monads haven't been in Haskell forever, many other ways have been tried before, but in the end turned out to be quite cumbersome and ugly in larger code bases. The introduction of monads was generally perceived as relieve in Haskell, and are becoming more and more popular in other non-lazy functional languages as well, such as OCaml.)
- drostie 10y agoTo expound on this connection: the basic idea is that a Promise has a `.then(fn)` method call which takes this `fn`, which returns a value. If that value is not a Promise, then it is lifted to a new promise; either way that promise is what's returned by the .then(). In Haskell, for type safety, you have to explicitly write this conversion and the function which does it has the somewhat-confusing name `return`. The `.then()` method then has the name `>>=`, pronounced "bind". If you have these, you have a template for type-safe functional metaprogramming. Given any x, you can produce "a program which produces an x" (return), and given any function from an x to a program which produces a y, you can bind that onto a program which produces an x, to produce a program which produces a y: return :: x -> ProgramWhichProduces x (>>=) :: ProgramWhichProduces x -> (x -> ProgramWhichProduces y) -> ProgramWhichProduces y This was huge historically because you can still metaprogram in a purely functional language, even though you cannot do things which have side-effects. So I can give you a pure primitive like `println :: String -> ProgramWhichProduces Nothing` and you can build `println("Hello, world!")` with it, and there are no side-effects in running that function on that input, so this is purely functional. In this way the act of actually running a `ProgramWhichProduces x` as an executable is deferred until the language is no longer in play: when the compiler is run, it looks for a `ProgramWhichProduces Nothing` called `main` and writes that out as the actual executable generated by the compiler. Monad refers then to precisely this design pattern of saying "I have some trivial embedding `x -> m x` into my problem domain `m` and I have some way to combine an `m x` with an `x -> m y` to produce an `m y`, hey, that's a monad!" ... One example is list comprehensions; there is a trivial way to embed any element as a singleton list [x] and then you can always use any function x -> [y] (called a "transducer" in Clojure) to process a list [x], concatenating together all of the [y]'s you get along the way. With ES6 generators this looks like: function* forEach(iterator_x, ys_from_x) { for (let x of iterator_x) { yield* ys_from_x(x); } } Hey, that's a monad. You can write a list comprehension like: // equivalent to Python's [f(x, y) for x of list_x for y of list_y if predicate(x, y)] forEach(list_x, x => forEach(list_y, y => predicate(x,y) ? [f(x, y)] : [])) In Haskell you can write all monads with a special syntax called `do`-notation; you can write the above in any of three ways: [f x y | x <- list_x, y <- list_y, predicate x y] -- which desugars to... do x <- list_x y <- list_y if predicate x y then [undefined] else [] return (f x y) -- which desugars to almost exactly the JS above... list_x >>= \x -> list_y >>= \y -> (if predicate x y then [undefined] else []) >>= \_ -> return (f x y) So that's the general pattern of monads and do-notation in a short, sweet lesson.
- BinaryIdiot 10y agoCorrect but once you get some functions returning promises and other functions returning actual values then your own code base can become confusing. I constantly find myself in these types of code bases trying to find function definitions so I know what it returns. You can try and make 100% of your asynchronous methods return promises but what happens when you need to work with a library that can't use promises? Do you write a callback for that? Now you have a mixture and sure a few of these no big deal but when the code base balloons and you start having one offs all over the place now you need to figure out what returns what and code specifically to that. Maybe if promises were in ECMAScript from the beginning then everyone would have used them and it wouldn't be a huge deal but right now it can get a bit frustrating, in my opinion. The stack traces of these nested promises are never fun to dive into as well though that likely depends on whether you're using a polyfill or not.
- atomwaffel 10y agoI'm not quite sure I understand you, but it sounds like a case for `Promise.resolve(value)`[1] – that's what I do whenever I don't know if something will be a Promise or an already-resolved value. `Promise.resolve(value)` returns a Promise that immediately resolves with `value` or whatever `value` resolves to if it's a Promise – no need to do any checks yourself, no need to nest Promises inside Promises inside Promises. For example, imagine you have a charting library and you want to draw a chart, but you don't know if the data is available already or if it's being loaded asynchronously. You can just write: // `drawChart(data)` is a synchronous function // `data` may be the data itself or a promise that resolves to the data Promise.resolve(data).then(drawChart) For me, this is actually the killer feature of Promises, avoiding callback hell is just a nice side-effect. Once you start thinking of Promises as placeholders for values that may or may not be known already (rather than just an abstraction of the `callback(err, value)` pattern), you can write some really elegant code. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- BinaryIdiot 10y agoFair enough that would take care of my issue for the most part. Native implementations of promises should work well with that. Seems most of the polyfills end up using setTimeout() which is kinda slow[1] and one of the reasons I don't typically like working with them (waiting for more ubiquitous support). Thanks for the info! [1] http://www.krissiegel.com/rants/2015/11/7/settimeout-sucks http://www.krissiegel.com/rants/2015/11/7/settimeout-sucks