1

I have a huge amont of data to transform into new format.

Actually I'm using map method but as it's syncronous and it's affecting performances.

dataFormatted = cmtAllRawdataDB[0].rows.map(elm => new Message(elm, configResult));

For information Message class have globally this format:

export class Data {
  public value: string;
  public date: Date;
  constructor(dbData) {
    this.value = '123';
  }
}

export class Measure {
  public name: string;
  public unit: string;
  public data: Data[];
  constructor(config, dbData) {
    this.name = config.name;
    this.unit = config.value;
    ...
    this.data = [new Data(dbData)];
  }
}

export class Sensor {
  public id: string;
  public label: string;
  public measures: Measure[] = [];
  constructor(dbData, config) {
    this.id = '123';
    this.label = 'SensorType';
    config.unitConfig.map(elm => this.measures.push(new Measure(elm, dbData)));
  }
}

export class Message {
  public id: string;
  ...
  public sensors: Sensor[];
  constructor(dbData: any, config: any) {
    this.id = dbData.value._id;
    ....
    this.sensors = [new Sensor(dbData, config)];
    console.log(this.id, this.arrivalTimestamp);
  }
}

Is there a way to run asynchronously this code ?

1

2 Answers 2

2

Just put this operation inside function and put it inside settimeout method, for just 10 millisecond

var example = () => {
  setTimeout(() => {
    return (dataFormatted = cmtAllRawdataDB[0].rows.map(
      elm => new Message(elm, configResult)
    ));
  }, 10);
};
Sign up to request clarification or add additional context in comments.

Comments

1

Use async and await keywords like this way

async getDataFormatted(){     return(cmtAllRawdataDB[0].rows.map(elm => new Message(elm, configResult)));
}

let dataFormatted= await getDataFormatted();

2 Comments

I'm trying to integrate your solution to my program. actually dataFormatted = cmtAllRawdataDB[0].rows.map(elm => new Message(elm, configResult)); is already inside a function (async function )
let dataFormatted= await getDataFormatted(); Only put this line in your main function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.