https://stackoverflow.com/a/50173415 has some good examples.
It's close, though. Really close. And don't get me wrong - I still love promises and async / await. They're much better than what we had before.
More clearly, Promise indeed forms a functor, if the domain is the set of Javascript functions which do not return 'Promise<X>'.
The only point of a Monad is that in a lazy language it desugars into an implicit nested functions, which guarantees the order of evaluation.
The monadic code, then, could be generalized over >>=, >> and return, and given that, specialized instances of a Monad class could be made, like Maybe, ST and what not.
The only meaning in performing these steps is to establish a guaranteed order of evaluation (via implicit nesting of functions) for serialized abstractions.
But that is ok, most of Javascript coders never studied CS fundamentals or comparative PL.
Please, do tell.
> But that is ok, most of Javascript coders never studied CS fundamentals or comparative PL.
Can you explain what you mean by this? You seem to be under the impression that javascript the language did something extra to make Promises form a monad when in reality, the very idea of a Promise forms a monad to begin with, independent of any particular implementation in javascript.
That is not true at all. You seem to confuse side-effect-free with lazy and the state monad with any monad. Monad instances like options or either data types allow for the simple composition of functions that yield them. In any language. Promises form a useful monad because there is no other way to talk about values that have not been evaluated yet.
There is a way, and it is known since at least R5RS.
https://www.gnu.org/software/mit-scheme/documentation/mit-sc...
To recapitulate some basics: JS is a strongly typed dynamic language.
Strongly typed implies no implicit coersions at runtime. Python is strongly typed. JS or PHP are weakly typed.
What's about this Python code:
1.0 + 1
Or this Scala Code: "1".toFloat + "1".toInt
In both cases that's clearly implicit type coercion at runtime.So according to the above constraint there would hardly be any strongly typed languages… (But quite the opposite is true in fact).
Weakly typed means you can hose the type-system somehow and then perform some "illegal" or completely nonsensical operation. For example that's easy in C, but not possible in JS. In a strongly typed language any value has some precise type at any time. This is true for JS so it's strongly typed.