Earlier quoted context omitted.
Consider async-await a syntactic sugar over Promises (from JavaScript). Then, Promises constitute an instance of the Monad typeclass where monadic `bind` or (>>=) is `Promise.then()`, and `return` is `Promise::resolve()`. Here is a translation of a modification of the example given in [1]: const promise1 = Promise.resolve(123); promise1.then(v => v * 2).then((value) => { console.log(value); // Expected output: 246 })…
Maybe I’m confused, but I dont see how Promise.then() corresponds to bind? If I understand correctly, the point of the bind function is you pass a callback which itself return the monad type. But the Promise.then() callback should not return a monad but just the regular result value of invoking the callback. So in essence Promise.then() is like Array.map() while bind is like Array.flatMap() Edit: It seems you are cor…
The behavior of the returned promise (call it p) depends on the handler's
execution result, following a specific set of rules. If the handler function:
* returns a value: p gets fulfilled with the returned value as its value.
[...]
* returns an already fulfilled promise: p gets fulfilled with that promise's value as its value.
... then you can obtain a solution closer to the Haskell translation by using the behaviour of the second cited bullet point from the MDN article: const promise1 = Promise.resolve(123);
promise1.then(v => Promise.resolve(v * 2)).then((value) => {
console.log(value);
// Expected output: 246
});
[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...