Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Rückgabe eines Wertes per return

    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

    Rückgabe eines Wertes per return

    This topic has been deleted. Only users with topic management privileges can see it.
    • bahnuhr
      bahnuhr Forum Testing Most Active last edited by

      Folgendes Script funktioniert.

      pruef_Wemos();
      
      function pruef_Wemos() {
          var test = akt_Version();
          log ("hier2 " + test);
      }
      
      function akt_Version() {
          var aa = 25
          log ("hier1 " + aa);
          return (aa);
      }
      

      Im log erscheint dann richtigerweise:

      javascript.0
      2023-04-17 13:59:31.340	info	script.js.Scripte.test7: hier2 25
      javascript.0
      2023-04-17 13:59:31.340	info	script.js.Scripte.test7: hier1 25
      

      Wenn ich dies aber nun probiere mit einem request in der 2. Funktion, also so:

      // Variablen
          request = require("request");
       
      pruef_Wemos();
      
      function pruef_Wemos() {
          var test = akt_Version();
          log ("hier2 " + test);
      }
      
      function akt_Version() {
          var options = {
              url: 'https://api.github.com/repos/arendst/Tasmota/releases/latest',
              headers: { 'User-Agent': 'ioBroker Tasmota Firmware Check' }
          };
          request(options, function (error, response, body) {
              if(error) { log('error: ' + error);
              } else {
                  var tasmotaJson = JSON.parse(body); var tasmotaTagName = tasmotaJson.tag_name; var tasmotaVersion = tasmotaTagName.replace(/v/i, "").trim();
                  log ("hier1 " + tasmotaVersion);
                  return (tasmotaVersion);
              }
              });
      }
      
      

      bekomme ich im log "undefined":

      javascript.0
      2023-04-17 14:01:40.582	info	script.js.Scripte.test8: hier1 12.5.0
      javascript.0
      2023-04-17 14:01:40.230	info	script.js.Scripte.test8: hier2 undefined
      

      Probiert habe ich schon async und wait.
      Klappen tut es nicht.

      Frage:
      Wie bekomme ich es hin, dass in "hier2" (also in der 1. Funktion) der Wert von der 2. Funktion ankommt.

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

        @bahnuhr

        so funktioniert es - aber evtl könnten profis das anders lösen 😞

        
        
        const axios=require('axios');
        
         
          pruef_Wemos(); 
         
         async function pruef_Wemos() {
           
             let test1 = await akt_Version().then(function(result)  {  log("hier2 " + result);})
          
        }
         
        async function akt_Version() {
            
        
         const backFromFunction = await axios.get('https://api.github.com/repos/arendst/Tasmota/releases/latest', {
                            headers: { 'User-Agent':`Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36` }
        }).then((result) => {
        
        
           log("hier1 " + result.data.tag_name)
           return  result.data.tag_name;
             
              
              
        }).catch((error) => {return error;});
        
         return backFromFunction
        }
        
        
        
        bahnuhr 1 Reply Last reply Reply Quote 1
        • bahnuhr
          bahnuhr Forum Testing Most Active @liv-in-sky last edited by

          @liv-in-sky

          Danke dir.
          Muss ich mir mal in Ruhe anschauen.

          Gelöst habe ich es nun indem ich alles in eine Funktion gepackt habe.

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

            @bahnuhr wollte ich auch schon vorschlagen 🙂

            1 Reply Last reply Reply Quote 0
            • F
              fastfoot last edited by

              ich würde es so lösen, await und then() gemixt macht für mich nicht wirklich Sinn, auch wenn es hier wohl funktioniert. Also entweder await oder then() verwenden(bei letzterem ist keine Rückgabe möglich, weshalb ich es fast nie verwende)

              const axios = require('axios');
              
              pruef_Wemos();
              
              async function pruef_Wemos() {
                  const test1 = await akt_Version();
                  log('hier2 ' + test1);
              }
              
              async function akt_Version() {
                  const url = 'https://api.github.com/repos/arendst/Tasmota/releases/latest';
                  const options = { headers: { 'User-Agent': `Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36` } }
                  try {
                      const res = await axios.get(url, options);
                      if (res.status === 200) {
                          log('hier1 ' + res.data.tag_name);
                          return res.data.tag_name;
                      }
                      else {
                          log('hier1 ' + res.status + ':' + res.statusText);
                          return res.statusText;
                      }
                  }
                  catch (e) {
                      log(e)
                  }
              }
              
              
              bahnuhr 1 Reply Last reply Reply Quote 1
              • bahnuhr
                bahnuhr Forum Testing Most Active @fastfoot last edited by

                @fastfoot
                Danke auch dir.
                Ich schau es mir an.

                Mit diesem await, promise, then steh ich noch ein bisschen auf Kriegsfuß.

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

                Support us

                ioBroker
                Community Adapters
                Donate

                410
                Online

                31.8k
                Users

                80.0k
                Topics

                1.3m
                Posts

                javascript
                3
                6
                216
                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