1

I have the following JS code and I'm trying to convert to aura component JS Controller, I'm getting error on the yield how would you make equivalent aura component code

    function* createChunkGenerator(
         itemList = [], chunkSize = 1, chunkCount = 0
    ) {
        chunkSize = Math.max(chunkSize, 1);

     while (itemList.length >= 1) {
       ++chunkCount;
       yield {
        chunkCount,
        itemList: itemList.splice(0, chunkSize),
      };
    }
}

const chunkGenerator = createChunkGenerator(data, 5);

Aura component javascript:

 createChunkGenerator: function( ) { 

    //....
 },

1 Answer 1

1

Aura doesn't support ES6 function generators, it only supports ES5 and ES6 Promises. Either import the function as a third-party library with ltng:require, or use a Lightning Web Component (LWC), which supports everything your browser supports (with limitations based on Locker Service or Lightning Web Security, whichever you're using). You can also write an LWC, and then call that LWC from Aura:

utils.js

import { LightningElement, api } from 'lwc';
export default class Utils extends LightningElement {
  @api *createChunkGenerator(
    itemList = [], chunkSize = 1, chunkCount = 0
  ) {
    chunkSize = Math.max(chunkSize, 1);
    while (itemList.length >= 1) {
      ++chunkCount;
      yield {
        chunkCount,
        itemList: itemList.splice(0, chunkSize),
      };
    }
  }
}

utils.html

<template></template>

Aura CMP

<aura:component>
  <c:utils aura:id="utils" />
</aura:component>

Aura Controller

myFunction: function(component, event, helper) {
  let generator = component.find("utils").createChunkGenerator(data, 5);
  // Rest of logic here
}
2
  • wow, how do you know all this lol Commented Apr 22, 2022 at 20:46
  • @ThreeOneThree Just a few decades of computer science experience, and a passion for learning new things 😁.
    – sfdcfox
    Commented Apr 22, 2022 at 21:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .