How can I increase the efficiency of this code?

Hey guys!
I need your help to make this code below much more efficient . I am running into a time-out in the backend right now.

I have 48 items to map and 8 queries running async right now!
How can I change the queries to run sync? Would it help?

export async function KalenderMAP(Result1, Result3, Wochentag) {
let KalenderAR = await Result1.items.map(item => {
let TimeSplitter = item.zeiten.split(":");
let hour = (TimeSplitter[0]);
let Minutes = (TimeSplitter[1]);
let Time = hour.padStart(2, '0') + ":" + Minutes.padStart(2, '0')
return LyraQ(item.zeiten, Result3, Wochentag).then((resLyra) => {
return OmegoQ(item.zeiten, Result3, Wochentag).then((resOmego) => {
return BaloQ(item.zeiten, Result3, Wochentag).then((resBalo) => {
return TymoQ(item.zeiten, Result3, Wochentag).then((resTymo) => {
return AmadeoQ(item.zeiten, Result3, Wochentag).then((resAmadeo) => {
return DiegoQ(item.zeiten, Result3, Wochentag).then((resDiego) => {
return PabloQ(item.zeiten, Result3, Wochentag).then((resPablo) => {
return PraxisQ(item.zeiten, Result3, Wochentag).then((resPraxis) => {
item.zeiten = Time
item.Lyra = resLyra
item.Omego = resOmego
item.Balo = resBalo
item.Tymo = resTymo
item.Amadeo = resAmadeo
item.Diego = resDiego
item.Pablo = resPablo
item.Praxis = resPraxis
return item
})
})})})})})})})})
return Promise.all(KalenderAR)
}
/////Geräte Funktionen ///////////////////////////////////////////////////////////
export async function LyraQ(Zeit, Result3, Wochentag) {
return wixData.query("Gruppentraining")
.limit(500)
.include("gerate", "kunde")
.hasSome("aktuellerBetreuer", Result3)
.eq("anfang", Zeit)
.eq("gerate", Lyra)
.eq("title", Wochentag)
.find()
.then((resGT) => {
if (resGT.length === 0) { resGT = ""
return resGT} else { resGT = resGT.items[0].kunde.fullName
return resGT }
})

Promise.all is not a good idea for this. The optimal answer would be to merge your collections into maybe one or two. Unless you have thousands of entries per each collection, you really should.

Btw, you’ve written your code in such a way that the Promise.all isn’t even doing anything (though it’s still not a good idea). Each then represents another sequential promise fulfilled so you have like 8 layers deep worth of promises, but your Promise.all is only executing one.

1 Like