[关闭]
@WrRan 2016-11-17T09:26:42.000000Z 字数 3246 阅读 1537

node-oracledb

19. Promises in node-oracledb

Node-oracledb supports Promises with all asynchronous methods. The native Promise
implementation is used in Node 0.12 and greater. Promise support is not
enabled by default in Node 0.10.

If an asynchronous method is invoked without a callback, it returns a
Promise:

  1. var oracledb = require('oracledb');
  2. oracledb.getConnection(
  3. {
  4. user : "hr",
  5. password : "welcome",
  6. connectString : "localhost/XE"
  7. })
  8. .then(function(conn) {
  9. return conn.execute(
  10. "SELECT department_id, department_name " +
  11. "FROM departments " +
  12. "WHERE manager_id < :id",
  13. [110] // bind value for :id
  14. )
  15. .then(function(result) {
  16. console.log(result.rows);
  17. return conn.close();
  18. })
  19. .catch(function(err) {
  20. console.error(err);
  21. return conn.close();
  22. });
  23. })
  24. .catch(function(err) {
  25. console.error(err);
  26. });

With Oracle's sample HR schema, the output is:

  1. [ [ 60, 'IT' ], [ 90, 'Executive' ], [ 100, 'Finance' ] ]

Notice there are two promise "chains": one to get a connection and the
other to use it. This is required because it is only possible to
refer to the connection within the function to which it was passed.

When invoking asynchronous methods, it is possible to accidentally
get a Promise by forgetting to pass a callback function:

  1. oracledb.getConnection(
  2. {
  3. user : "hr",
  4. password : "welcome",
  5. connectString : "localhost/WRONG_SERVICE_NAME"
  6. });
  7. . . .

Since the returned promise will not have a catch block, as the
developer intended to use the callback programming style, any
rejections that occur will go unnoticed. Node.js 4.0 added the
unhandledRejection event to prevent such rejections from going
unnoticed:

  1. process.on('unhandledRejection', (reason, p) => {
  2. console.error("Unhandled Rejection at: ", p, " reason: ", reason);
  3. // application specific logging, throwing an error, or other logic here
  4. });
  5. oracledb.getConnection(
  6. {
  7. user : "hr",
  8. password : "welcome",
  9. connectString : "localhost/WRONG_SERVICE_NAME"
  10. });
  11. . . .

Whereas the code without the unhandledRejection exception silently
exited, adding the handler could, for example, show:

  1. $ node myapp.js
  2. Unhandled Rejection at: Promise {
  3. <rejected> [Error: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
  4. ] } reason: [Error: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
  5. ]

19.1 Custom Promise Libraries

The Promise implementation is designed to be overridden, allowing a
custom Promise library to be used. An external library can also be
used to add Promise support to Node 0.10.

  1. var mylib = require('myfavpromiseimplementation');
  2. oracledb.Promise = mylib;

Promises can be completely disabled by setting

  1. oracledb.Promise = null;

If your code uses the promise style in Node 0.10 but you have not
installed your own promise library then you will get an error like:

  1. $ node mypromiseapp.js
  2. node_modules/oracledb/lib/util.js:53
  3. throw new Error(getErrorMessage(errorCode, messageArg1));
  4. ^
  5. Error: NJS-009: invalid number of parameters
  6. at Object.assert (node_modules/oracledb/lib/util.js:53:11)
  7. at Oracledb.getConnection (node_modules/oracledb/lib/oracledb.js:71:12)
  8. at Oracledb.getConnection (node_modules/oracledb/lib/util.js:72:19)
  9. at Object.<anonymous> (mypromiseapp.js:8:10)
  10. at Module._compile (module.js:456:26)
  11. at Object.Module._extensions..js (module.js:474:10)
  12. at Module.load (module.js:356:32)
  13. at Function.Module._load (module.js:312:12)
  14. at Function.Module.runMain (module.js:497:10)
  15. at startup (node.js:119:16)

Because node-oracledb Promises support is not enabled by default when
using Node 0.10, the callback API is expected. The error stack trace
indicates that line 10 of mypromiseapp.js forgot to pass the
callback. Either install your own Promise library or use the callback
programming style.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注