使用asynchronousdb编程来sorting动作

新的asynchronous和挣扎。 在下面的例子中,我希望初始化一个表,然后通过以下方式处理它的内容:a)删除旧数据b)插入新loggingc)将表读入数组d)显示数组

'use strict'; // ================================================================================ // Module dependencies var pgp = require('pg-promise')(); // ================================================================================ //Configure the database connection var config = { user: 'user', //env var: PGUSER database: 'database', //env var: PGDATABASE password: 'password', //env var: PGPASSWORD }; var db = pgp(config); // ================================================================================ // Initialise rhe variables var customers = []; // ================================================================================ // Initialise table db.none("DELETE FROM testing") .then(function(data) { console.log("Deleted old records"); // success; }) .catch(function(error) { console.log(error); }); db.none("INSERT INTO testing (firstname, surname) VALUES ('Bob', 'Brown')") .then(function(data) { console.log("Inserted new record"); // success; }) .catch(function(error) { console.log(error); }); // ================================================================================ // Display records db.any("SELECT * FROM testing") .then(function(data) { console.log("Looping records"); data.forEach(function(row, index, data) { customers.push(row.firstname, row.lastname); console.log(row); }); }) .catch(function(error) { console.log(error); }); console.log("The customers are:"); console.log(customers); 

输出不像预期的那样,但有点像预期的那样。 有趣的是,在“插入新logging”之后,在命令提示符被重新调用之前有30秒的等待时间。

 The customers are: [] Looping records anonymous { id: 3, firstname: 'Bob', surname: 'Brown', created: 2016-08-03T01:43:34.880Z } Deleted old records Inserted new record 

我的问题最终是使用asynchronous编程,当然有一些场景需要按照上面的例子顺序执行,在这种场景下,如何在asynchronous环境(如node.js)中进行编码。

由于您正在使用支持promise访问数据库的库,因此您应该在下一个.then方法中执行每一步。 如果不执行.then方法中的步骤,那么每个语句都会在当前的“tick”上执行,直到没有更多的“tick”语句为止。 asynchronous方法(使用db.none(...)调用db.none(...)将在未来的“tick”上执行。这就是您将最后2个console.log语句看作是第一个输出的方法。

尝试改变你的代码到下面的东西,以获得更好的工作stream程:

 'use strict'; // ================================================================================ // Module dependencies var pgp = require('pg-promise')(); // ================================================================================ //Configure the database connection var config = { user: 'user', //env var: PGUSER database: 'database', //env var: PGDATABASE password: 'password', //env var: PGPASSWORD }; var db = pgp(config); // ================================================================================ // Initialise rhe variables var customers = []; // ================================================================================ // Initialise table db.none("DELETE FROM testing") .then(function(data) { console.log("Deleted old records"); // success; return db.none("INSERT INTO testing (firstname, surname) VALUES ('Bob', 'Brown')"); }) .then(function(data) { console.log("Inserted new record"); // success; // Display records return db.any("SELECT * FROM testing"); }) .then(function(data) { console.log("Looping records"); data.forEach(function(row, index, data) { customers.push(row.firstname, row.lastname); console.log(row); }); }) .then(function() { console.log("The customers are:"); console.log(customers); }) .catch(function(error) { console.log(error); }); 

看看每个行动/步骤是如何在另一个方法。 另外,因为db. 方法返回promise,你可以返回.then方法和下一个方法,然后当这个语句完成时执行。

希望这可以帮助。

解决scheme将取决于查询之间是否存在依赖关系。 如果他们是依赖的,你可以按照你的承诺链接他们; 否则可以批量并行执行它们:

 'use strict'; var promise = require('bluebird'); var pgp = require('pg-promise')({ promiseLib: promise // use a custom promise library }); var config = { user: 'user', //env var: PGUSER database: 'database', //env var: PGDATABASE password: 'password', //env var: PGPASSWORD }; var db = pgp(config); function prepareCustomers(t) { return t.batch([ t.none('DELETE FROM testing'), t.none('INSERT INTO testing VALUES($1, $2)', ['Bob', 'Brown']), t.any('SELECT * FROM testing') ]) .then(data=>data[2]); // get results from the select query } db.tx(prepareCustomers) .then(customers=> { console.log(customers); }) .catch(error=> { console.log(error); }) .finally(pgp.end); // only call pgp.end when exiting the application 

而且,当你在这样的数据库中进行更改时,通常会使用一个事务来处理这个事务,如上例所示。

有趣的是,在“插入新logging”之后,在命令提示符被重新调用之前有30秒的等待时间。

请参阅库去初始化