Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. js callback asynchron problem

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    js callback asynchron problem

    This topic has been deleted. Only users with topic management privileges can see it.
    • hanss
      hanss @OliverIO last edited by

      @OliverIO
      Vielen Dank, aber Deine Lösung ist nicht sicher, weil zeitabhängig.
      Ein synchroner setState wäre die Lösung, das hätte mir zwei Tage rumprobieren erspart.

      OliverIO 1 Reply Last reply Reply Quote 0
      • OliverIO
        OliverIO @hanss last edited by

        @hanss wie gesagt bei asynchron gibt es keine garantierte abfolge.

        evtl beschreibst du mal was du am ende erreichen möchtest, evtl ist dein bisheriger lösungsweg nicht optimal

        1 Reply Last reply Reply Quote 0
        • AlCalzone
          AlCalzone Developer last edited by AlCalzone

          Eventuell sind die neuen async-Funktionen in 4.8.x was für dich @hanss
          https://forum.iobroker.net/topic/36999/neu-diverse-async-funktionen-im-javascript-adapter

          Damit lässt sich der Programmablauf trotz asynchronität schön steuern. Ich hab den obigen Schnipsel mal entsprechend umgebaut. Beachte das async vor der Funktion und await setStateAsync:

          async function raumzaehler() {
          	var Raumctr = 0;
          
          	// Schleife für jeden Raum ######################################
          	for (let i = 0; i < S_Raum.length; i++) {
          		const element = S_Raum[i];
          		if (SollTemp_akt != Temp_FOA && SollTemp_akt != Temp_Abw) {
          			await setStateAsync(Out_Root + D_Raum[index] + ".Man", SollTemp_akt, true);
          			setRT(index, element, SollTemp_akt); // function setRT wg. Callback
          			log("RaumzählerCB: " + Raumctr + " Index: " + RaumIndex);
          		} else {
          			setRT(index, element, SollTemp_akt);
          			log("Raumzähler: " + Raumctr + " Index: " + RaumIndex);
          		}
          	}
          	// Schleife für jeden Raum ######################################
          
          	// ##########   Script soll hier fortfahren, wenn  Schleife für jeden Raum durchlaufen ist.  ##########
          	log("Raumzähler nach allen Räumen: " + Raumctr + " Index: " + RaumIndex);
          
          }
          
          // Funktion aufrufen nicht vergessen!
          raumzaehler();
          
          liv-in-sky OliverIO 2 Replies Last reply Reply Quote 0
          • liv-in-sky
            liv-in-sky @AlCalzone last edited by liv-in-sky

            @AlCalzone

            ich habe immer wieder probleme mit dem async und await - toll wäre es, wenn man so was wie mit dem type-script kurs für adapterentwickler machen würde - nur halt für dieses thema

            AlCalzone F 2 Replies Last reply Reply Quote 0
            • AlCalzone
              AlCalzone Developer @liv-in-sky last edited by

              @liv-in-sky https://gist.github.com/AlCalzone/d14b854b69ce5e8a03718336cc650a95
              😎

              liv-in-sky 1 Reply Last reply Reply Quote 1
              • liv-in-sky
                liv-in-sky @AlCalzone last edited by

                @AlCalzone danke werd ich mal durcharbeiten

                1 Reply Last reply Reply Quote 0
                • F
                  fastfoot @liv-in-sky last edited by

                  @liv-in-sky sagte in js callback asynchron problem:

                  ....wenn man so was wie mit dem type-script kurs für adapterentwickler machen würde....

                  gibt es dazu einen Link?

                  liv-in-sky 1 Reply Last reply Reply Quote 0
                  • liv-in-sky
                    liv-in-sky @fastfoot last edited by

                    @fastfoot

                    https://forum.iobroker.net/topic/36493/09-09-20-30-einsteiger-cursus-demonstration-von-typescript

                    F 1 Reply Last reply Reply Quote 1
                    • F
                      fastfoot @liv-in-sky last edited by

                      @liv-in-sky cool, Danke!

                      1 Reply Last reply Reply Quote 0
                      • OliverIO
                        OliverIO @AlCalzone last edited by

                        @AlCalzone

                        Async und await machen aus asynchronen Funktionen wieder synchrone Funktionen.
                        Das will er ja nicht

                        OliverIO hanss AlCalzone 3 Replies Last reply Reply Quote 0
                        • OliverIO
                          OliverIO @OliverIO last edited by OliverIO

                          Hier mal ein kurzer Artikel, der auf einen Punkt eingeht, der die Funktionsweise der asynchronen Funktionen (callback oder auch promise basiert) erklärt

                          https://dev.to/steelvoltage/if-javascript-is-single-threaded-how-is-it-asynchronous-56gd

                          1 Reply Last reply Reply Quote 0
                          • hanss
                            hanss @OliverIO last edited by

                            @OliverIO
                            Vielen Dank, ich habe das jetzt so gelöst:

                            In die Funktion setRT() einen Raumzähler und das Script eingefügt, wenn alle Räume durchlaufen sind:
                            Das ist sehr übersichtlich und nicht so kompliziert wie async und await.
                            Ich glaube, das kapier ich nicht mehr.

                            setRT() {
                              Raumctr++
                              //mein Script
                            
                               if (S_Raum.length == Raumctr){              // letzter Raum
                                   // Script hier, wenn alle Räume durchlaufen sind 
                                   Raumctr=0        
                               }
                            }
                            
                            1 Reply Last reply Reply Quote 0
                            • AlCalzone
                              AlCalzone Developer @OliverIO last edited by AlCalzone

                              @OliverIO wieso nicht? Wenn man etwas ausführen will, nachdem asynchrone Vorgänge durch sind, ist async/await genau das richtige Mittel.
                              Und IMO intuitiver als der callback Krempel. Nach dem Aufruf mit await ist es einfach fertig.

                              OliverIO 1 Reply Last reply Reply Quote 0
                              • OliverIO
                                OliverIO @AlCalzone last edited by

                                @AlCalzone
                                Ah hatte einer seiner Antworten falsch interpretiert. Hab nochmal gelesen.
                                Wenn er mit seiner Lösung nun zufrieden ist, ist ja alles gut

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post

                                Support us

                                ioBroker
                                Community Adapters
                                Donate

                                453
                                Online

                                31.8k
                                Users

                                80.0k
                                Topics

                                1.3m
                                Posts

                                5
                                16
                                773
                                Loading More Posts
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes
                                Reply
                                • Reply as topic
                                Log in to reply
                                Community
                                Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                                The ioBroker Community 2014-2023
                                logo