Search code examples
javascriptknex.js

What is the difference between createTable(...).createTable(...) and createTable(...).then(...) in knex.js?


I am using knex.js to connect to a MySQL database. I want to create a couple of tables. So far, I have seen two styles. One of them chains the use of createTablelike so,

    knex.schema.createTable('foo',function(table){...}).createTable('bar'...)

The other style uses the then mechanism to do the same thing.

    knex.schema.createTable('foo',function(table){...}).then(function() {
        knex.schema.createTable.createTable('bar'...)

Are they semantically similar? Can I assume they do the same thing? Will both of them execute the second clause once the first clause completely finishes?


Solution

  • I think a comment should be enough as answer, but

    then
    

    in Javascript returns a promise which is executed asynchronously, while the other method is executed synchronously and your javascript won't get to the next line until the line is executed.