[Help] Understanding ReScript promises unwrapping

Hi all!

Lately I’m learning ReScript, but there’s something really unclear to me about promises. Given the following snippet

const doSomethingElse = (val) => new Promise((resolve) => resolve(val))
const doSomething = () => new Promise((resolve) => resolve("done"))

const taskList = doSomething().then(res => {
  console.log(`First task ${res}`);
  
  return doSomethingElse(res);
})

const main = (async () => {
  const result = await taskList
  console.log(`All tasks: ${result}`)
})();

I tried to write the equivalent in ReScript, as it follows

let doSomethingElse = (res) => Js.Promise2.make(((~resolve, ~reject as _) => resolve(. res)))
let doSomething = () => Js.Promise2.make(((~resolve, ~reject as _) => resolve(. "done")))

let taskList = doSomething()
  ->Js.Promise2.then(async res => {
    Js.log(`First task: ${res}`)

    let result = await doSomethingElse(res)
    result
  });

let main = (async () => {
  let result = await taskList
  Js.log(`All tasks: ${result}`)
})();

but the results I’m getting are different, since in the ReScript version I’m getting All tasks: undefined as output, but I don’t really understand why.

Thanks for any help,

Best!

Which ReScript version are you on? There was an issue where undefined was returned in certain cases when using Js.Promise2 (but not in Js.Promise) in versions prior to 10.1.3, see here: Promise returned undefined instead of value (10.0.1 & 10.1.1) · Issue #5958 · rescript-lang/rescript-compiler · GitHub

1 Like

From the rescript installed package

{
  "name": "rescript",
  "version": "10.1.2",
  ...
}

I installed it following the Installation Guide by cloning the rescript-project-template. So it definitely be related to the ReScript version which leads me to the next question: how can I update it? Deleting node modules and reinstalling apparently isn’t enough.

Edit: upgrading to 10.1.4 solved the issue, thanks!

Best!

Sorry, I phrased my answer wrongly. I wanted to state that it got fixed in 10.1.3, so only the versions 10.0.x - 10.1.2 are affected.

Glad that it works for you now.

1 Like