项目作者: cebor

项目描述 :
ES2015 Promisify Library
高级语言: JavaScript
项目地址: git://github.com/cebor/es-promisify.git
创建时间: 2018-01-10T19:54:52Z
项目社区:https://github.com/cebor/es-promisify

开源协议:MIT License

下载


ES2015 Promisify Library

NPM version
NPM downloads
Build Status

This library enables node-styled functions to work as Promises through the use of ES2015 syntax.

Examples

Single-valued callback with multiple parameters

For a snipped like the following:

  1. 'use strict';
  2. const Promisify = require('es-promisify');
  3. // Test a promise that returns a single value
  4. function sum (a, b, callback) {
  5. callback(null, a + b);
  6. }
  7. async function main () {
  8. let sumPromise = Promisify(sum);
  9. console.log(await sumPromise(2, 2));
  10. }
  11. main();

We’d expect to have an output like this one:

  1. 4

Multiple-valued output with multiple arguments

For a snippet like the following

  1. 'use strict';
  2. const Promisify = require('es-promisify');
  3. // Test return of multiple values
  4. function multiOutput (a, b, callback) {
  5. callback(null, a, b);
  6. }
  7. async function main () {
  8. let multiOutputPromise = Promisify(multiOutput);
  9. console.log(await multiOutputPromise('a', 'b');
  10. }
  11. main();

We’d expect to have an output like this one:

  1. ['a', 'b']

Function bound to an object state

For a snippet like the following:

  1. 'use strict';
  2. const Promisify = require('es-promisify');
  3. // Test binding of methods inside objects
  4. class Bindable {
  5. constructor () {
  6. this._prop = 3;
  7. }
  8. get prop () {
  9. return this._prop;
  10. }
  11. increment (callback) {
  12. this._prop += 1;
  13. callback(null, this.prop);
  14. }
  15. }
  16. async function main () {
  17. let bindable = new Bindable();
  18. let incrementPromise = Promisify(bindable.increment, bindable);
  19. console.log(await incrementPromise());
  20. }
  21. main();

We’d expect an output like this one:

  1. 4