NEWS
Rückgabe eines Wertes per return
-
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. -
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 }
-
Danke dir.
Muss ich mir mal in Ruhe anschauen.Gelöst habe ich es nun indem ich alles in eine Funktion gepackt habe.
-
@bahnuhr wollte ich auch schon vorschlagen
-
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) } }
-
@fastfoot
Danke auch dir.
Ich schau es mir an.Mit diesem await, promise, then steh ich noch ein bisschen auf Kriegsfuß.