Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Entwicklung
    4. Mehrere requests syncron laufen lassen

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    Mehrere requests syncron laufen lassen

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

      Hallo Leute, ich sitze gerade an einem neuen Adapter wo ich 6 http requests abwarten muss bis ich alle Daten habe um weiter zu machen. wie kann ich das am besten umsetzen mit cb finde ich ein bisschen unelegant da dann am ende da die dann ja alle ineinander verschachtelt sind. mit timeout ist auch nicht so schön, gibt es hier was elegantes wo ich nicht drauf komme? mfg Tobi

       mqttcloud.userdata = function (cb) {
              _get('GET', 'users/me', function (data) {
                  console.log(JSON.stringify(data))
                  ownData = data;
                  if (typeof cb === "function") cb(data);
      
                  return data
              });
          }
          mqttcloud.userdevices = function (cb) {
              _get('GET', 'product-items', function (data) {
                  ownDevices = data;
                  console.log(JSON.stringify(data))
                  if (typeof cb === "function") cb(data);
              });
          }
          mqttcloud.usercert = function (cb) {
              _get('GET', 'users/certificate', function (data) {
                  ownCert = data;
                  console.log(JSON.stringify(data))
                  if (typeof cb === "function") cb(data);
              });
          }
          mqttcloud.devices = function (cb) {
              _get('GET', 'products', function (data) {
                  deviceList = data;
                  console.log(JSON.stringify(data))
                  if (typeof cb === "function") cb(data);
              });
          }
          mqttcloud.boards = function (cb) {
              _get('GET', 'boards', function (data) {
                  boardList = data;
                  console.log(JSON.stringify(data))
                  if (typeof cb === "function") cb(data);
              });
          }
          function _get(method, path, cb) {
      
              if (ACCESS_TOKEN === '' || ACCESS_TYPE === '') {
                  _getticket(function () {
                      _get(method, path, cb)
                  });
                  return;
              }
              var headers = {
                  "Content-Type": "application/json",
                  "Authorization": ACCESS_TYPE + " " + ACCESS_TOKEN,
              };
      
              var options = {
                  host: URL,
                  path: PATH + path,
                  port: 443,
                  method: method,
                  headers: headers
              };
      
              var req = https.request(options, function (res) {
                  var body = "";
      
                  res.setEncoding('utf8');
                  res.on('data', function (d) { body += d });
                  res.on('end', function () { cb(JSON.parse(body)) });
              });
              req.on('error', function (e) { this.adapter.log.error("api errror " + e) });
              req.end();
          };
      

      hier mal die request funktionen

      1 Reply Last reply Reply Quote 0
      • apollon77
        apollon77 last edited by

        Callbacks mit einem Zähler geht, die bei jedem Response erniedrigt wird. Ansonsten wäre die Antwort von AlCalzone garantiert Promises 🙂

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

          da ich mit der zeit gehen will und mich promises interessiert, versuche ich das mal…

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

          Support us

          ioBroker
          Community Adapters
          Donate

          710
          Online

          31.7k
          Users

          79.8k
          Topics

          1.3m
          Posts

          2
          3
          308
          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