purescript-promises icon indicating copy to clipboard operation
purescript-promises copied to clipboard

Promise flattening

Open hdgarrood opened this issue 5 years ago • 0 comments

Because promises of promises are automatically "flattened", any nesting of promises is likely to lead to runtime errors. Consider:

module Main where

import Prelude
import Effect (Effect)
import Effect.Console (logShow)
import Effect.Promise (Promise)
import Effect.Promise as Promise
import Effect.Promise.Console as Promise.Console

main = Promise.runPromise logShow logShow do
  hello <- Promise.resolve (Promise.resolve "hello")
  hello' <- hello
  Promise.Console.log (hello' <> ", world")

which, when run, outputs this:

TypeError: promise.then is not a function
    at Object.exports.thenImpl (/home/harry/code/ps-scratch/output/Effect.Promise/foreign.js:2:18)
    at /home/harry/code/ps-scratch/output/Effect.Promise/index.js:55:29
    at /home/harry/code/ps-scratch/output/Data.Function/index.js:16:24
    at /home/harry/code/ps-scratch/output/Main/index.js:11:70

I'm not sure if this is fixable but it may be worth documenting, at least.

hdgarrood avatar May 16 '20 15:05 hdgarrood