Skip to content

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

Closed
@El-Fitz

Description

@El-Fitz

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);
            })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions