I am trying to make a calendar array in Node.js.
this is my main.mts:
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
dayjs.extend(customParseFormat);
function nextDay(d: string) {
const e = dayjs(d, 'MMM D, YYYY (ddd)');
const f = dayjs(e).add(1, 'day');
return dayjs(f).format('MMM D, YYYY (ddd)');
}
export class CalendarEdit {
private cal: string[] = [];
constructor(d: string) {
this.cal.push(d);
this.init(365).catch(err => { throw err; });
}
public addDate() {
const d = this.cal[this.cal.length - 1];
const e = nextDay(d);
this.cal.push(e);
}
private init(j: number) {
this.addDate();
if (j > 1)
this.init(j - 1);
}
public printCal() {
console.log(this.cal);
}
}
but result says every element in cal array is same, which has to be last day of calendar.
I know that JavaScript is basically non-blocking language, but I cannot find a way.
I tried callbacks and promises, and async, await pairs.
but none of them worked.
I googled it but I cannot find a solution does work on my case. anyone knows the answer?
EDIT: I call CalendarEdit in my index.mts like this:
import { CalendarEdit } from './main';
let cal = new CalendarEdit('Jun 1, 2022 (Wed)');
cal.printCal();
I edited my addDate() to this and it solved error:
public addDate() {
const d = nextDay(this.cal[this.cal.length - 1]);
this.cal.push(d);
}