Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Beispiele mit Promises

    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

    Beispiele mit Promises

    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Marty56 last edited by Marty56

      Ich experimentieren ein bisschen mit Promisses herum und möchte einen synchronen setState() machen. Das ist meine Lösung bisher

      function setState_sync(id,value) {
        return new Promise(function(resolve, error) {
          setState (id,value,function(){
              setTimeout(function() {resolve();}, 1000);
          });
        });
      }
      (async () => {
          await setState_sync("Test",false);
          log("State hat: " + getState("Test").val);
      })();
      

      Ich verzögere um 1 Sekunde, bis ich den State abfrage. So super finde ich das nicht. Ich fände es besser, bis ich eine Acknowledge von der Datenbank (in meinem Fall redis) bekommen würde.
      Gibt es da eine bessere Lösung?

      1 Reply Last reply Reply Quote 0
      • M
        Marty56 last edited by Marty56

        Noch ein anderes Beispiel

        Nutzung: Wiederholte Ausgabe einer Warnung, bis zum Eintreffen eines Abbruchs.

        //-------- Klasse zur wiederholten Ausgabe von Warnungen z.b.  auf den Lautsprechern -----
        
        function sleep(ms) {
          return new Promise(resolve => setTimeout(resolve, ms));
        }
        class Repeat_Warning {
          constructor() {
            this.text = "";
            this.duration = 0;
            this.repeat = true;
          }
          start(text,duration) {
            this.text = text;
            this.duration = duration;  
            this.repeat = true;
            (async () => {
                while (this.repeat) {
               // hier kann die individuelle Text2Speech Ausgabe stehen durch log ersetzt
                    log("this.text");
                    await sleep(this.duration);
                }
            })();
          }
          stop() {
            this.repeat = false;
          }
        }
        //Anwendung
        var Warning = new Repeat_Warning();
        // Der Text "Ausgabe Test!" wird mit einer Frequenz von 2 Sekunden wiederholt
        // ausgegeben und nach 10 Sekunden wieder beendet
        
        (async () => {
            Warning.start("Ausgabe Test!",2000);
            await sleep(10000);
            Warning.stop();
        })();
        
        1 Reply Last reply Reply Quote 0
        • N
          Nahasapee last edited by

          ich hatte mal ne Wrapperklasse gebastelt
          in der ich setstate und createstate
          verwurstet habe, findest du hier:

          https://github.com/Nahasapeemapetilon/MyTelegramMenu/blob/master/lib/iobrokerfunctionswithpromise.js

          genutzt habe ich es zum Beispiel hier:
          https://github.com/Nahasapeemapetilon/MyTelegramMenu/blob/master/lib/menuitemreport.js
          Viele Grüße

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

          Support us

          ioBroker
          Community Adapters
          Donate

          869
          Online

          31.8k
          Users

          80.0k
          Topics

          1.3m
          Posts

          javascript
          2
          3
          443
          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