Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
¿Por qué mi código no se ejecuta sincrónicamente cuando no uso ningún método asíncrono?

He creado la siguiente clase en TS:

 import MbusMaster, { SerialOptions } from "node-mbus"; { ... } export class MbusReader { private countersToRead: Counter[]; constructor(countersToRead: Counter[]) { this.countersToRead = countersToRead; } readData(connectionOptions: SerialOptions): Counter[] { const mbusmaster = new MbusMaster(connectionOptions); const returnValue: Counter[] = []; mbusmaster.connect(); this.countersToRead.forEach(counter => { mbusmaster.getData(counter.mbusAddress,(err:string,data:any) => { const valuedCounter: Counter = { ...counter }; valuedCounter.relevantRecords.forEach((dr,index) => { const value = data.DataRecord.filter((rec:any) => rec.id === dr.id)[0].Value; valuedCounter.relevantRecords[index] = { ...valuedCounter.relevantRecords[index], value: value * dr.scaleFactor }; }) returnValue.push(valuedCounter); console.log(returnValue) }) }) mbusmaster.close(); return returnValue; } }

Ahora en mi archivo index.ts lo uso así:

 const reader = new MbusReader(counters); const res = reader.readData(options).length; console.log(res);

Usando ts-node obtengo el siguiente resultado:

 0 [ { mbusAddress: 1, name: 'test', relevantRecords: [ [Object] ] } ]

¿Por qué mi código no se ejecuta sincronizado? ¿Qué me estoy perdiendo?

paquete npm utilizado: node-mbus

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!