• Ahh - yes, Promises can be a bit crazy... You can do something like this (this one just checks BTN1 and resolves if it was pressed when it checked, or times out after a while):

    // Promise.resolve() is just to get it started - in reality you'd stick this on the
    // end of the promise chain
    Promise.resolve().then(function() { 
      return new Promise(function(resolve, reject) {
        var n = 20;
        var i = setInterval(function() {
          console.log("Checking BTN1 "+n+"/20...");
          if (BTN1.read()) {
            clearInterval(i);
            resolve();
          }
          if (n-- <= 0) {
            clearInterval(i);
            reject("Timeout!");
          }
        }, 1000);
      });  
    }).then(function() {
      console.log("Done!");
    });
    

    Basically when you do stuff like this you want to return a promise immediately (which you do with new Promise) then you execute your code in the function inside new Promise(function(..) where you have two arguments you can call - one to resolve it if everything went ok, and one to reject if it's all gone wrong and you want to bail out :)

  • Gordon, thanks for the code snippet. I was able to incorporate the CEREG poll into the bg96 connect function. Now it powers the module, waits 5 seconds then polls the module for the cell to connect. This makes it startup a bit faster.

About

Avatar for Kartman @Kartman started