NodeJS, SQL Async issue

I'm having a problem serializing a couple of functions in nodeJS. What I want to do is create a temporary view, read it, then drop it for each element in the ft array. Here is my code. I am using the following function in NodeJS. The Db is a sqlite3 plugin for node.

function(){
 ft.forEach(function(fti){
  var view="create temporary view ..."+fti;
  db.exec(view,aftaView)
  console.log("created View "+fti);
 });
};
function aftaView(err){   
  if (err) console.log(err+" K ");      
  participantID.forEach(function(prt){
   Obj.forEach(function(bj){
     console.log("Object: "+bj);
      //perform second query here
     db.all("select ...", {$pID:prt,$obj:bj}, function(err,rows){
      if (err) throw err;
      rows.forEach(function (row) {
         //console.log(row);
       });
       //perform third function here, in readiness for second iterarion of the very                                                                          //           first loop
        db.run("drop view Kemp");
      });
 });

});
};

Here is the console output:

created View 1
created View 2
created View 3
created View 4
created View 5
created View 6

//fails

Error: SQLITE_ERROR: view already exists

I clearly need to make sure the second function runs for each item in the first loop, I just don't know how. Any help will be appreciated

Your problem is probably a different one that the one you think you're having. It is not that afterView isn't run for each item, rather that all the views are created before the first instance of afterView is run at all. This comes from the async nature of methods taking callbacks (don't know about the framework/library you are using, but this is the norm) in javascript.

What you need to do is to make sure you only create one view at a time, and wait until afterView has finished before you continue with the next one.