I have a function (that contains promises internally so it itself runs synchronously) that seems to be running asynchronously within my main code. No matter how I format my promise it seems like the resolve gets sent before the functions ends execution:
This problem is also logically recursive, in that if I try adding another promise around the nameExists function (within this very promise) and then putting the resolve in a 'then', i just run into the same issue with the nested resolve...
    document.getElementById("config-select").addEventListener("input", function(){
      //check if the doc name exists: returns doc id
      //promise that doc_obj is created before moving on
      let doc_obj = {};
      let promise = new Promise(function (resolve, reject) {
        let doc_name = document.getElementById("config-select").value;
        doc_obj = nameExists(doc_name);
        resolve('done'); //this executes BEFORE nameExists is done processing...bringing back the original asynch issue i was trying to fix in the first place...
      });
      promise.then(function (result) {
          alert("then: "+doc_obj);
          if(doc_obj.bool === true){//it does exist:
            alert("replacing id");
            document.getElementById("config-select").setAttribute("doc-id", doc_obj.id);
          }
          else{//it doesn't:
            alert("resetting id");
            document.getElementById("config-select").setAttribute("doc-id", "");
          }
        }
      );
    });
The nameExists function:
//check if the name in config-select is an existing doc (assumes name is a unique document field)
const nameExists = function(name){
  //get all docs
  localDB.allDocs({include_docs: true}).then(function (result) {
    //return object set to default state if no match is found
    let doc_obj = {bool: false, id: ""};
    alert("Entering the match checker...");
    for(let i =0; i<result.total_rows; i++) {
      if(result.rows[i].doc.name == name){
        alert(result.rows[i].doc.name);
        alert(name);
        doc_obj.bool = true;
        doc_obj.id = result.rows[i].doc._id;
        //found a match
        break;
      }
    }
    //return the result
    alert("returned obj.id: "+doc_obj.bool);
    return doc_obj;
  }).catch(function (err) {console.log(err);});
};
Ideally, I would like the doc_obj or some return value object to be populated with data from the nameExists function, before evaluating my 'if statements'. How can I format my promise/resolve statement to achieve this?
 
     
    