I try to make a function (custom command in nightwatch.js), that waits for an elements existence but waits maximum 10 seconds for that. The code in the .execute function is running in an console. I see that its not working because after the 4th time calling this function javascript execution isnt working anymore.
    function waitForElement(selector, scriptName) {
    return this
        .execute(function(sel) {    
            var found = false;    
            function myAsyncRequest() {
              return new Promise((resolve) => {
                var start = Date.now();
                if( $(sel).length || Date.now() - start > 10000 ) {
                    // found = true;
                    resolve();
                }    
              })
            }    
            myAsyncRequest()
              .then( function() {
                found = true;
              })
                 return found;           
        }, 
        [selector], 
        function(result) {
              if ( result.value === true ){
                  console.log( selector + " gefunden.");
              }
              else {
                  throw new Error(selector + " nicht gefunden in der Testdatei " + scriptName );    
              }           
        });
}
