用工小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.8 KiB

6 months ago
  1. # MZ - Modernize node.js
  2. [![NPM version][npm-image]][npm-url]
  3. [![Build status][travis-image]][travis-url]
  4. [![Test coverage][coveralls-image]][coveralls-url]
  5. [![Dependency Status][david-image]][david-url]
  6. [![License][license-image]][license-url]
  7. [![Downloads][downloads-image]][downloads-url]
  8. Modernize node.js to current ECMAScript specifications!
  9. node.js will not update their API to ES6+ [for a while](https://github.com/joyent/node/issues/7549).
  10. This library is a wrapper for various aspects of node.js' API.
  11. ## Installation and Usage
  12. Set `mz` as a dependency and install it.
  13. ```bash
  14. npm i mz
  15. ```
  16. Then prefix the relevant `require()`s with `mz/`:
  17. ```js
  18. var fs = require('mz/fs')
  19. fs.exists(__filename).then(function (exists) {
  20. if (exists) // do something
  21. })
  22. ```
  23. With ES2017, this will allow you to use async functions cleanly with node's core API:
  24. ```js
  25. const fs = require('mz/fs')
  26. async function doSomething () {
  27. if (await fs.exists(__filename)) // do something
  28. }
  29. ```
  30. ## Promisification
  31. Many node methods are converted into promises.
  32. Any properties that are deprecated or aren't asynchronous will simply be proxied.
  33. The modules wrapped are:
  34. - `child_process`
  35. - `crypto`
  36. - `dns`
  37. - `fs` (uses `graceful-fs` if available)
  38. - `readline`
  39. - `zlib`
  40. ```js
  41. var exec = require('mz/child_process').exec
  42. exec('node --version').then(function (stdout) {
  43. console.log(stdout)
  44. })
  45. ```
  46. ## Promise Engine
  47. `mz` uses [`any-promise`](https://github.com/kevinbeaty/any-promise).
  48. ## FAQ
  49. ### Can I use this in production?
  50. Yes, Node 4.x ships with stable promises support. For older engines,
  51. you should probably install your own promise implementation and register it with
  52. `require('any-promise/register')('bluebird')`.
  53. ### Will this make my app faster?
  54. Nope, probably slower actually.
  55. ### Can I add more features?
  56. Sure.
  57. Open an issue.
  58. Currently, the plans are to eventually support:
  59. - New APIs in node.js that are not available in older versions of node
  60. - ECMAScript7 Streams
  61. [bluebird]: https://github.com/petkaantonov/bluebird
  62. [npm-image]: https://img.shields.io/npm/v/mz.svg?style=flat-square
  63. [npm-url]: https://npmjs.org/package/mz
  64. [github-tag]: http://img.shields.io/github/tag/normalize/mz.svg?style=flat-square
  65. [github-url]: https://github.com/normalize/mz/tags
  66. [travis-image]: https://img.shields.io/travis/normalize/mz.svg?style=flat-square
  67. [travis-url]: https://travis-ci.org/normalize/mz
  68. [coveralls-image]: https://img.shields.io/coveralls/normalize/mz.svg?style=flat-square
  69. [coveralls-url]: https://coveralls.io/r/normalize/mz?branch=master
  70. [david-image]: http://img.shields.io/david/normalize/mz.svg?style=flat-square
  71. [david-url]: https://david-dm.org/normalize/mz
  72. [license-image]: http://img.shields.io/npm/l/mz.svg?style=flat-square
  73. [license-url]: LICENSE
  74. [downloads-image]: http://img.shields.io/npm/dm/mz.svg?style=flat-square
  75. [downloads-url]: https://npmjs.org/package/mz