Execute many functions at once!

74 views
Skip to first unread message

Hasan A Yousef

unread,
Dec 28, 2013, 1:36:17 AM12/28/13
to part...@googlegroups.com
Hi Peter.
Can I execute/call many functions at one in partial?

I want to call many SQL statements at the first run of the application to create all the required tables, this can be done in one long file, can I device this into multi files, so that each one executing a function to create one or set of related tables to be created!
Unless if you have better idea for doing this.

Regards,
Hasan

Peter Širka

unread,
Dec 28, 2013, 8:45:45 AM12/28/13
to part...@googlegroups.com
Hi Hasan yes.

You have two options:

2. use partial.js Array prototype waiting:

function view_homepage() {
   
var self = this;
   var arr = [];
 
   arr
.push(1);
   arr
.push(2);
   arr
.push(3);
   arr
.push(4);

   arr
.waiting(function(current, next) {

      setTimeout
(function() {
          console
.log(current);
         
next();
     
}, current * 100);

   
}, function() {
      console
.log('COMPLETE');
     
self.empty();
   
});
}



Dňa sobota, 28. decembra 2013 7:36:17 UTC+1 Hasan A Yousef napísal(-a):

Hasan A Yousef

unread,
Dec 28, 2013, 10:11:18 AM12/28/13
to part...@googlegroups.com
Great... both are nice, but I liked the  "partial.js Array prototype waiting" more :)
Message has been deleted

Hasan A Yousef

unread,
Feb 1, 2014, 10:06:41 AM2/1/14
to tot...@googlegroups.com, part...@googlegroups.com
Hi Peter,,
In my application, I want to create all the SQL tables at the first run, upon entering the DB parameters, the below function is run,, 
As you see, for creating 2 tables only (considering the SQL statements defined as global parameters), many lines need to be re-written which will make this file long..
do you think there is a way to optimize it, or what I'm doing is correct.


function post_setup() {
    var self = this;
    var dbConfig = framework.module('storage').get('dbServer');
    self.module('storage').set('dbServer', { host: self.post.host, port: self.post.port, DataBase: self.post.database,
     user: self.post.user, password: self.post.pswd});

    var connection = self.database();

    connection.connect(
        function(err){ 
            if(err != null) { self.view500(err); return; }
        connection.query(self.global.CreatUsrsTbl, function(err, result){
            if (err != null) { self.view500(err); }
            else {            
                connection.query(self.global.CreatItemsTbl, function(err, result){
                if (err != null) { self.view500(err); }
                else {
                    self.redirect('/');
                }
                });         
            }
        connection.end();
        });
    });        
}


Peter Širka

unread,
Feb 2, 2014, 8:17:48 AM2/2/14
to tot...@googlegroups.com, part...@googlegroups.com
Hi Hasan,

I published a new version of total.js. New version supports models, so rewrite your global variables into the some models (self.global.CreatUsrsTabl, CreatItemsTbl).

My tip:

Create model, example:

user.js:

var SQL = '...';

exports
.create = function(db, params, callback) {
    db
.query(SQL, callback);
};

item.js:

var SQL = '...';

exports
.create = function(db, params, callback) {
    db
.query(SQL, callback);
};

Controller:

function post_setup() {

   
var self = this;

   
....
   
....
   
var connection = self.database();


    connection
.connect(function(err) {
 
       
self.model('user').create(connection, params, function(err, result) {
       
             
// ...
             
// ...

       
});
   
});
}


I updated total.js on GitHub and currently contains two methods for node harmony:

async(function *() {

   
var user = sync(self.model('user').create)(connection, params);
   
var item = sync(self.model('item').create)(connection, params);

    console
.log(user, item);

});

You must install node.js v0.11.*, example on GitHub:

Thanks!


Hasan A Yousef

unread,
Feb 2, 2014, 11:18:10 AM2/2/14
to tot...@googlegroups.com
Thanks Peter..
What are the 'Params', I run the same code, and got an error: 'Params are not defined'.

Regards

Message has been deleted
Message has been deleted

Peter Širka

unread,
Feb 2, 2014, 11:53:55 AM2/2/14
to tot...@googlegroups.com
Params ... I mean your custom params (example POST data).
You can remove this param.

Hasan A Yousef

unread,
Feb 2, 2014, 12:37:07 PM2/2/14
to tot...@googlegroups.com
Hi Peter..
The first part worked perfectly with me, 
but the second part "async(function *() { }",it gave me this error: 
async(function *() {
                      ^
SyntaxError: Unexpected token *



below code worked fine:
    var connection = self.database();

    connection.connect(function(err) {
        self.model('users').create(connection, function(err, result) {
            if (err != null) { self.view500(err);}
            else {
                console.log('result: ', result.rows);
            }
        });
    });


Peter Širka

unread,
Feb 2, 2014, 4:13:09 PM2/2/14
to tot...@googlegroups.com
Hi Hasan,
yes, you must install http://blog.nodejs.org/2014/01/28/node-v0-11-11-unstable/ ... And run with --harmony flag:

node --harmony index

This is new JavaScript feature.
Thanks

Hasan A Yousef

unread,
Feb 2, 2014, 10:38:47 PM2/2/14
to tot...@googlegroups.com
Thanks Peter..
The output was"
user:  function (cb) {
callback = cb;
if (!executed && params) {
executed = true;
callback.apply(this, params);
}
} item:  function (cb) {
callback = cb;
if (!executed && params) {
executed = true;
callback.apply(this, params);
}
}
What this means!

Hasan Yousef

unread,
Feb 2, 2014, 11:58:32 PM2/2/14
to tot...@googlegroups.com
Thanks Peter.
I run it without Param, as shown, and got the below output!


async(function *() {

   
var user = sync(self.model('user').create)(connection);
   
var item = sync(self.model('item').create)(connection);

    console
.log(user, item);

});

Peter Širka

unread,
Feb 3, 2014, 7:08:25 AM2/3/14
to tot...@googlegroups.com
I must see your code. Output is bad.
Thanks.

Hasan A Yousef

unread,
Feb 3, 2014, 10:16:08 AM2/3/14
to tot...@googlegroups.com
Attached both: my application and the console printscreen
rectoPG.zip
async.png

Peter Širka

unread,
Feb 3, 2014, 10:57:20 AM2/3/14
to tot...@googlegroups.com
You must use yield keyword:

var user = yield sync(self.model('users').create)(connection);
var item = yield sync(self.model('items').create)(connection);

Hasan A Yousef

unread,
Feb 3, 2014, 11:21:20 AM2/3/14
to tot...@googlegroups.com
PERFECT..
Thanks a lot Peter, this will make my life easy :)
Reply all
Reply to author
Forward
0 new messages