Skip to content
  • Home
  • Recent
  • Tags
  • 0 Unread 0
  • Categories
  • Unreplied
  • Popular
  • GitHub
  • Docu
  • Hilfe
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
ioBroker Logo

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. JavaScript
  5. Rückgabe eines Wertes per return

NEWS

  • UPDATE 31.10.: Amazon Alexa - ioBroker Skill läuft aus ?
    apollon77A
    apollon77
    48
    3
    8.9k

  • Monatsrückblick – September 2025
    BluefoxB
    Bluefox
    13
    1
    2.2k

  • Neues Video "KI im Smart Home" - ioBroker plus n8n
    BluefoxB
    Bluefox
    16
    1
    3.4k

Rückgabe eines Wertes per return

Scheduled Pinned Locked Moved JavaScript
javascript
6 Posts 3 Posters 243 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • bahnuhrB Online
    bahnuhrB Online
    bahnuhr
    Forum Testing Most Active
    wrote on last edited by
    #1

    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.


    Wenn ich helfen konnte, dann Daumen hoch (Pfeil nach oben)!
    Danke.
    gute Forenbeiträge: https://forum.iobroker.net/topic/51555/hinweise-f%C3%BCr-gute-forenbeitr%C3%A4ge
    ScreenToGif :https://www.screentogif.com/downloads.html

    liv-in-skyL 1 Reply Last reply
    0
    • bahnuhrB bahnuhr

      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-skyL Offline
      liv-in-skyL Offline
      liv-in-sky
      wrote on last edited by
      #2

      @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
      }
      
      
      

      nach einem gelösten Thread wäre es sinnvoll dies in der Überschrift des ersten Posts einzutragen [gelöst]-... Bitte benutzt das Voting rechts unten im Beitrag wenn er euch geholfen hat. Forum-Tools: PicPick https://picpick.app/en/download/ und ScreenToGif https://www.screentogif.com/downloads.html

      bahnuhrB 1 Reply Last reply
      1
      • liv-in-skyL liv-in-sky

        @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
        }
        
        
        
        bahnuhrB Online
        bahnuhrB Online
        bahnuhr
        Forum Testing Most Active
        wrote on last edited by
        #3

        @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.


        Wenn ich helfen konnte, dann Daumen hoch (Pfeil nach oben)!
        Danke.
        gute Forenbeiträge: https://forum.iobroker.net/topic/51555/hinweise-f%C3%BCr-gute-forenbeitr%C3%A4ge
        ScreenToGif :https://www.screentogif.com/downloads.html

        liv-in-skyL 1 Reply Last reply
        0
        • bahnuhrB bahnuhr

          @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-skyL Offline
          liv-in-skyL Offline
          liv-in-sky
          wrote on last edited by
          #4

          @bahnuhr wollte ich auch schon vorschlagen :-)

          nach einem gelösten Thread wäre es sinnvoll dies in der Überschrift des ersten Posts einzutragen [gelöst]-... Bitte benutzt das Voting rechts unten im Beitrag wenn er euch geholfen hat. Forum-Tools: PicPick https://picpick.app/en/download/ und ScreenToGif https://www.screentogif.com/downloads.html

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fastfoot
            wrote on last edited by
            #5

            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)
                }
            }
            
            

            iobroker läuft unter Docker auf QNAP TS-451+
            SkriptRecovery: https://forum.iobroker.net/post/930558

            bahnuhrB 1 Reply Last reply
            1
            • F fastfoot

              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)
                  }
              }
              
              
              bahnuhrB Online
              bahnuhrB Online
              bahnuhr
              Forum Testing Most Active
              wrote on last edited by
              #6

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

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


              Wenn ich helfen konnte, dann Daumen hoch (Pfeil nach oben)!
              Danke.
              gute Forenbeiträge: https://forum.iobroker.net/topic/51555/hinweise-f%C3%BCr-gute-forenbeitr%C3%A4ge
              ScreenToGif :https://www.screentogif.com/downloads.html

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              Support us

              ioBroker
              Community Adapters
              Donate

              543

              Online

              32.4k

              Users

              81.5k

              Topics

              1.3m

              Posts
              Community
              Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen | Einwilligungseinstellungen
              ioBroker Community 2014-2025
              logo
              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Home
              • Recent
              • Tags
              • Unread 0
              • Categories
              • Unreplied
              • Popular
              • GitHub
              • Docu
              • Hilfe