Asynchronous function (How do that with primises?

42 views
Skip to first unread message

tec...@estudioaureo.com

unread,
Nov 6, 2016, 1:31:18 AM11/6/16
to nodejs
I have a question about asynchronous function. Here my function "My_function":

function My_function (my_name, callback){
      stmt = db.prepare ("SELECT number_table1 from my_table1 WHERE user=?");
        stmt.bind(my_name);
        stmt.get(function(error,row){
          if(error){
            throw err;
          }
          else{
            if(row){
              callback(number_table1);
            }
            else{
              console.log("error");
            }
           }
       });
    }
Work fine but I have 2 tables and I need do other query and I need add two numbers so... in my function I need do too this query:
stmt = db.prepare ("SELECT number_table2 from my_table2 WHERE user=?");

and finally return back in my callback "number_table1 + number_table2". Somebody know how to solve it? Thanks in advance.

Best regards!

Emanuele DelBono

unread,
Nov 6, 2016, 5:12:44 PM11/6/16
to nod...@googlegroups.com
You could try with something like this:


function My_function (my_name, callback){
      stmt = db.prepare ("SELECT number_table1 from my_table1 WHERE user=?");
        stmt.bind(my_name);
        stmt.get(function(error,row){
          if(error){
            throw err;
          }
          else{
            if(row){
              var stmt2 = db.prepare ("SELECT number_table2 from my_table2 WHERE user=?");
              stmt2.bind(my_name);
              stmt2.get(function(error2,row2){
                callback(number_table1 + numer_table2);
             }
          })
       });
    }
I also think (but don't know which Db and driver are you using) that the results (number_table1 and number_table2) should be somewhere in row object.

bye


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/6a1a408f-92e3-4303-8264-5716591e5f99%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

waheng chang

unread,
Dec 6, 2016, 12:05:55 PM12/6/16
to nodejs
You can check my Medium, I write blog about javascript and node.js.


var My_function = function(my_name) {
  return new Promise(function(resolve, reject) {
    stmt = db.prepare ("SELECT number_table1 from my_table1 WHERE user=?");
      stmt.bind(my_name);
      stmt.get(function(error,row){
        if(error){
          throw err;
        }
        else{
          if(row){
            resolve(number_table1);
          }
          else{
            reject("error");
          }
         }
     });
  })
}

My_function.then(function(number_table1){
  console.log(number_table1)
},function(error){
  console.error(error)
})

Reply all
Reply to author
Forward
0 new messages