Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # pn
  2. [![NPM][NPM1]][NPM2]
  3. [![Build Status][1]][2]
  4. The `pn` library gives you the Promise-using node standard library
  5. you've always dreamed of. Think "Promised Node" when saying it.
  6. Just about every node standard library method that takes a callback will now
  7. return a `Promise` iff no callback is supplied. But existing code
  8. that passes callbacks will still work fine --- and fast: No unnecessary
  9. `Promise`s are created if you supply a callback.
  10. The wrappers are generated automatically by a script, with a table to
  11. handle exceptions and odd cases. See below for more details.
  12. ## Installation
  13. ```
  14. npm install pn
  15. ```
  16. ## Usage
  17. ```
  18. var fs = require('pn/fs');
  19. fs.writeFile('foo', 'bar', 'utf-8').then(function() { console.log('done'); });
  20. // But you can use callbacks, too -- existing code won't break.
  21. fs.writeFile('foo', 'bat', 'utf-8', function(err) { console.log('yay'); });
  22. ```
  23. This library uses node native promises (ie `global.Promise`) by
  24. default, and thus works with node >= 0.11.
  25. You can use [`core-js`](https://www.npmjs.org/package/core-js) or
  26. [`es6-shim`](https://www.npmjs.org/package/es6-shim)
  27. to add ES6 Promises to earlier versions of node, for example:
  28. ```
  29. require('es6-shim');
  30. var fs = require('pn/fs');
  31. ```
  32. Just be sure that `es6-shim` is loaded before the `pn` package in that
  33. case.
  34. You might also want to look into packages like
  35. [`prfun`](https://www.npmjs.org/package/prfun)
  36. which add some helpers to make working with native promises much more
  37. fun.
  38. In particular, the `Promise#done` method is very useful when
  39. debugging, at least until v8's native Promise debugging
  40. capabilities are completed.
  41. ## Custom Promise types
  42. You can also specify a custom `Promise` type to use, as follows:
  43. ```
  44. var MyPromise = require('prfun'); // Use prfun's Promises, for example.
  45. require('pn/_promise')(MyPromise); // This only needs to be done once.
  46. ```
  47. ## Exceptions and odd cases
  48. The wrappers are automatically generated by `scripts/generate.js`;
  49. there is a table in that file which specifies all the odd cases.
  50. In general: if the node API has a callback which takes multiple
  51. value arguments, the `Promise` returned will be an object with
  52. named fields corresponding to the different values. If the node
  53. API takes a callback *and* returns a value, `pn` will return
  54. the original value with a nonenumerable field named `promise`
  55. corresponding to the callback. Combining these two cases:
  56. ```
  57. var child_process = require('pn/child_process');
  58. var cp = child_process.execFile('true');
  59. console.log('pid', cp.pid);
  60. cp.promise.then(function(result) {
  61. console.log('stdout: ', result.stdout);
  62. console.log('stderr: ', result.stderr);
  63. });
  64. ```
  65. * `child_process`: The `exec` and `execFile` methods promise a object
  66. with fields named `stdout` and `stderr`. They return a `ChildProcess`
  67. object with a nonenumerable field named `promise` corresponding to the
  68. callback.
  69. * `crypto`: The `randomBytes` and `pseudoRandomBytes` methods are
  70. now always asynchronous, returning a `Promise` if no callback
  71. is supplied. Use the new `randomBytesSync` and `pseudoRandomBytesSync`
  72. methods if you want synchronous computation. *This is backwards
  73. incompatible with existing node code.*
  74. * `dns`: The `lookupService` method promises an object with
  75. fields named `hostname` and `service`.
  76. * `fs`: The `exists` method doesn't pass an error to its callback.
  77. The promisied version will never reject. The `write` method promises
  78. an object with fields named `written` and `data`. The `read` method
  79. promises an object with fields named `read` and `data`.
  80. * `http`, `https`: The `request` and `get` methods return a `ClientRequest`
  81. object with a nonenumerable field named `promise`, which will
  82. resolve to an `IncomingMessage` object.
  83. * `process`: You can defer computation to the next tick with
  84. `require('pn/process').nextTick().then(function(){...})`
  85. * `tls`: The `connect` and `createServer` return objects with a
  86. nonenumerable field named `promise`.
  87. There are a few object methods which are not promisified by this
  88. package:
  89. * `domain`: `Domain#bind`, `Domain#intercept`
  90. * `http`,`https`: `ClientRequest#setTimeout`, `IncomingMessage#setTimeout`,
  91. `Server#setTimeout`, `ServerResponse#setTimeout`, `Server#listen`,
  92. `Server#close`
  93. * `net`: `Server#listen`, `Server#close`, `Server#getConnections`,
  94. `Socket#write`, `Socket#setTimeout`
  95. * `readline`: `Interface#question`
  96. * `stream`: `Writable#write`, `Writable#end`
  97. * `dgram`: `Socket#send`, `Socket#bind`.
  98. ## Related packages
  99. Here are some other packages with similar aims:
  100. * [`promised-node`](https://www.npmjs.org/package/promised-node)
  101. * [`then-fs`](https://www.npmjs.org/package/then-fs)
  102. * [`final-fs`](https://www.npmjs.org/package/final-fs)
  103. ## License
  104. Copyright (c) 2014-2018 C. Scott Ananian
  105. Permission is hereby granted, free of charge, to any person obtaining a copy
  106. of this software and associated documentation files (the "Software"), to deal
  107. in the Software without restriction, including without limitation the rights
  108. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  109. copies of the Software, and to permit persons to whom the Software is
  110. furnished to do so, subject to the following conditions:
  111. The above copyright notice and this permission notice shall be included in
  112. all copies or substantial portions of the Software.
  113. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  114. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  115. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  116. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  117. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  118. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  119. THE SOFTWARE.
  120. [NPM1]: https://nodei.co/npm/pn.png
  121. [NPM2]: https://nodei.co/npm/pn/
  122. [1]: https://travis-ci.org/cscott/node-pn.svg
  123. [2]: https://travis-ci.org/cscott/node-pn