Search code examples
javascriptpromiseexportundefined

What is wrong with my code? I am stuck. I am trying to export some promise results


I am trying to export an object to another module which includes a promise. I want results of the promises. I can get the result of promises, I just can't export. Or maybe I did everything wrong in this function.

I am building an application for people to create their own interactive stories. I get a link, a heading, a story beginning, some story choices and story parts. I was able to store them and sort them. I've cloned the original arrays, so I could modify the code safely. Those clone arrays are the results of promises which are taken from the local storage. I've turned local storage functions to async functions so there is no problem about that. The promises were ready and I've created this final function where I stuck.

export const sendToHell = {
  res: [],
  kamino: async function () {
    this.res = await Promise.all([
      stored.storedLink,
      stored.storedHeading,
      stored.storedBeginning,
      stored.storedChoices,
      stored.storedParts,
    ]);
    return this.res;
  },
  cloneLink: [...sendToHell.res[0]],
  cloneHeading: [...sendToHell.res[1]],
  cloneBeginning: [...sendToHell.res[2]],
  cloneChoices: [...sendToHell.res[3]],
  cloneParts: [...sendToHell.res[4]],
};

sendToHell.kamino();

The error I am getting here is 'Cannot access sendToHell beofre init.' What I actually need from this code is, modified clone arrays. When I used "this" key word. I get an another error. which is 'Cannot read properties of undefined reading res'

I've tried top level await but for a reason I don't know it doesn't work. "I know top level await only works in modules." To be more clear. "Stored" is an array which includes other arrays like stored link, stored heading... "StoredHeading", "StoredBeginning"... Those are arrays coming from local storage. They are resolved promises. "CloneHeading", "CloneBeginning" ... These will be the arrays which will be clones of Stored arrays and exported to another module.

Finally I know that exports and imports are synchronous. And if I write them in global scope they will be executed first while my promise is waiting. I've also tried the immediately executed function. Thank you for your time. Thank you for your time .


Solution

  • sendToHell is an anti-pattern of Promise.all -

    const [
      cloneLink,                     // <Value 1>
      cloneHeading,                  // <Value 2>
      cloneBeginning,                // <Value 3>
      cloneChoices,                  // <Value 4>
      cloneParts,                    // <Value 5>
    ] = await Promise.all([
      stored.storedLink,             // <Promise 1>
      stored.storedHeading,          // <Promise 2>
      stored.storedBeginning,        // <Promise 3>
      stored.storedChoices,          // <Promise 4>
      stored.storedParts,            // <Promise 5>
    ])
    

    If you insist on a wrapper function -

    function kamino(stored) {
      return Promise.all([
        stored.storedLink,           // <Promise 1>
        stored.storedHeading,        // <Promise 2>
        stored.storedBeginning,      // <Promise 3>
        stored.storedChoices,        // <Promise 4>
        stored.storedParts,          // <Promise 5>
      ])
    }
    
    export { kamino }
    
    import { kamino } from "./path/to/kamino.js"
    
    const [
      cloneLink,                     // <Value 1>
      cloneHeading,                  // <Value 2>
      cloneBeginning,                // <Value 3>
      cloneChoices,                  // <Value 4>
      cloneParts,                    // <Value 5>
    ] = await kamino(stored)