Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise returned undefined instead of value (10.0.1 & 10.1.1) #5958

Closed
3 of 5 tasks
El-Fitz opened this issue Jan 27, 2023 · 4 comments
Closed
3 of 5 tasks

Promise returned undefined instead of value (10.0.1 & 10.1.1) #5958

El-Fitz opened this issue Jan 27, 2023 · 4 comments
Milestone

Comments

@El-Fitz
Copy link

El-Fitz commented Jan 27, 2023

Thank you for filing! Check list:

  • Is it a bug? Usage questions should often be asked in the forum instead.
  • Concise, focused, friendly issue title & description.
  • A minimal, reproducible example.
  • OS and browser versions, if relevant.
  • Is it already fixed in master?

===========================================

Hi!

I've noticed, in some conditions, some promises returning "undefined" instead of the value they should return.
I got it working by using a different syntax, but I thought you might be interested.

I've got the following code

// Adapter.res

@genType
type repositoryResult = string

@genType
type serviceResult = string

type adapter = {
  firstAction: Inputs.firstAction => promise<serviceResult>,
  otherAction: Inputs.otherAction => promise<array<serviceResult>>,
}

let mapResult = (repositoryResult: repositoryResult) => repositoryResult

@genType
let make = (repository: repository): adapter => {
  firstAction: (input: Inputs.firstAction) =>
    input->repository.doSomething->then(result => result->mapResult->resolve),
  otherAction: (input: Inputs.otherAction) =>
    input->repository.otherAction->then(outputs => {
      // outputs the expected values
      Js.log2("outputs", outputs)
      Belt.Array.map(outputs, mapResult)->resolve;
  }),
}

Which compiles to

// Adapter.mjs

// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Curry from "rescript/lib/es6/curry.js";
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
import * as Js_promise2 from "rescript/lib/es6/js_promise2.js";

var Inputs = {};

function mapResult(repositoryResult) {
  return repositoryResult;
}

function make(repository) {
  return {
          firstAction: (function (input) {
              return Js_promise2.then(Curry._1(repository.firstAction, input), (function (result) {
                            return Promise.resolve(result);
                          }));
            }),
          otherAction: (function (input) {
              return Js_promise2.then(Curry._1(repository.otherAction, input), (function (summaries) {
                            console.log("outputs", outputs);
                            return Promise.resolve(Belt_Array.map(outputs, mapResult));
                          }));
            })
        };
}

export {
  Inputs ,
  mapResult ,
  make ,
}
/* No side effect */

In another module, I use it like this:

// UseCase.res

let doSomething = (
  dependencies: dependencies,
  {input, idealChunkLength, idealLength, idealCount}: params,
) =>
  input
  ->ChunkingUtil.chunkInput(idealChunkLength)
  ->Belt.Array.map(({paragraphs}) => textBlocksToText(paragraphs))
  ->(
    chunks => 
      Adapter.make(dependencies.textSummarisationAdapterRepository).otherAction({
        chunks chunks,
        idealLength,
        idealCount,
      })->then(results => {
        // outputs undefined
        Js.log2("Results: ", results)
        mergeOutputs(results)->resolve
      })
  )

Interestingly, Js.log2("outputs", outputs) in Adapter.res prints the expected values.
But Js.log2("Results: ", results) prints undefined (and the rest of the logic falls apart afterwards accordingly).
And it was the case using both 10.0.1 (with@ryyppy/rescript-promise) and 10.1.1 (with Js.Promise2)

However, when updating the otherAction in Adapter.res to use async / await syntax, like below, it all works as expected, and both Js.log2 output the expected values.

@genType
let make = (repository: repository): adapter => {
  ...
  otherAction: async (input: Inputs.otherAction) => {
    let summaries = await input->repository.otherAction
    Js.log2("outputs", outputs)
    Belt.Array.map(outputs, mapResult);
  }
}

which compiles to

function make(repository) {
  return {
          ...
          otherAction: (async function (input) {
              var outputs = await Curry._1(repository.otherAction, input);
              console.log("outputs", outputs);
              return Belt_Array.map(outputs, mapResult);
            })
}
@cristianoc
Copy link
Collaborator

cristianoc commented Jan 27, 2023

Would you be able to give a self-contained example (can be compiled without any additional code), with only the minimal code strictly needed to observe the difference. (E.g. firtstAction is not actually used so could be omitted), perhaps with 2 functions otherAction and otherAction2 to show the difference when calling one vs the other.

@El-Fitz
Copy link
Author

El-Fitz commented Jan 28, 2023

Sure! I'll try to that over the weekend

@eshurakov
Copy link

eshurakov commented Feb 9, 2023

I'm also experiencing something similar (ReScript 10.1.2).

Example code:

Js.Promise2.resolve("hello")
->Js.Promise2.then(v => {
  Js.log(v)
  Js.Promise2.resolve("hello 2")
})
->Js.Promise2.then(v => {
  Js.log(v)
  Js.Promise2.resolve("hello 3")
})
->ignore

When run it prints

hello
undefined

And I expect it to print:

hello
hello 2

Compiled code:

var Js_promise2 = require("rescript/lib/js/js_promise2.js");

Js_promise2.then(Js_promise2.then(Promise.resolve("hello"), (function (v) {
  console.log(v);
  return Promise.resolve("hello 2");
})), (function (v) {
        console.log(v);
        return Promise.resolve("hello 3");
      }));

If I try to log the result of calling the Js_promise2.then

console.log(Js_promise2.then(Promise.resolve("hello"), (function (v) {
  return Promise.resolve("hello 2");
})))

I get undefined.


I also get undefined if I run the code sample from here: https://rescript-lang.org/docs/manual/latest/promise#access-the-contents-and-transform-a-promise

@cristianoc
Copy link
Collaborator

Silly mistake. Fix in #5996 will be in the next 10.3 bug fix release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants