I was looking at the below block of code. I am trying to understand how can there be a catch without try block in javascript. I understand there is a promise being used, but the create database is within the promise and my understanding is that we should have had the try block surrounding the client.queryAsync.
The below code works fine and I am trying to understand how it works without try block. I also read http://know.cujojs.com/tutorials/async/mastering-async-error-handling-with-promises but the link also shows try block being used.
Can anyone please explain. Thanks in advance
function init(dbName) {
return util.serialize('Open database', function () {
return connect(maintenanceDbName).then(function (client) {
return client.queryAsync('CREATE DATABASE ' + dbName + ' TEMPLATE=template0 ENCODING=\'UTF8\' LC_COLLATE=\'C\' LC_CTYPE=\'C\';').catch(function (err) {
// Already created previously, which is fine
if (err.message.indexOf('already exists') < 0) {
throw err;
}
}).finally(function () {
return closeDatabase(client);
});
}).then(function () {
return connect(dbName);
}).then(function (client) {
db = client;
return fs.readFileAsync(path.join(__dirname, 'postgresql.sql'), 'utf8').then(db.queryAsync.bind(db));
});
});
}