Add promisify (#47)

This commit is contained in:
Jaco Greeff
2017-12-12 20:17:45 +01:00
committed by GitHub
parent b659622384
commit c612440bcd
5 changed files with 97 additions and 2 deletions
+18
View File
@@ -49,4 +49,22 @@ Returns a `Logger` that has `.log`, `.error` and `.warn` methods. Loggins is don
const l = require('@polkadot/util/logger')('test');
l.log('blah'); // <date> TEST: blah
```
## promisify
Wraps an async callback into a `Promise`
```js
function promisify (fn: (any) => void, ...params: Array<any>): Promise<any>
```
Wraps the supplied async function `fn` that has a standard JS callback `(error: Error, result: any)` into a `Promise`, passing the supplied parameters. When `error` is set, the Promise is rejected, else the Promise resolves with the `result` value.
```js
const promisify = require('@polkadot/util/promisify');
await promisify((a, cb) => cb(null, a), true); // resolves with `true`
await promisify((cb) => cb(new Error('error!'))); // rejects with `error!`
```
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util",
"version": "0.7.1",
"version": "0.8.0",
"description": "A collection of useful utilities for @polkadot",
"main": "index.js",
"keywords": [
+2 -1
View File
@@ -12,11 +12,12 @@ const jsonrpc = require('./jsonrpc');
const keccak = require('./keccak');
const logger = require('./logger');
const number = require('./number');
const promisify = require('./promisify');
const u8a = require('./u8a');
/**
@summary Utility methods for this package are split into groups
*/
module.exports = Object.assign(
{}, array, { assert }, bn, buffer, ext, hex, is, jsonrpc, keccak, { logger }, number, u8a
{}, array, { assert }, bn, buffer, ext, hex, is, jsonrpc, keccak, { logger }, number, { promisify }, u8a
);
+33
View File
@@ -0,0 +1,33 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const assert = require('./assert');
const isFunction = require('./is/function');
/**
@name promisify
@signature function promisify (fn: (any) => void, ...params: Array<any>): Promise<any>
@summary Wraps an async callback into a `Promise`
@description
Wraps the supplied async function `fn` that has a standard JS callback `(error: Error, result: any)` into a `Promise`, passing the supplied parameters. When `error` is set, the Promise is rejected, else the Promise resolves with the `result` value.
@example
const promisify = require('@polkadot/util/promisify');
await promisify((a, cb) => cb(null, a), true); // resolves with `true`
await promisify((cb) => cb(new Error('error!'))); // rejects with `error!`
*/
module.exports = function promisify (fn: (any) => void, ...params: Array<any>): Promise<any> {
assert(isFunction(fn), 'Expected function input to promisify');
return new Promise((resolve, reject) => {
fn.apply(null, [].concat(params, [
(error: Error, result: any): void => {
if (error) {
reject(error);
} else {
resolve(result);
}
}
]));
});
};
+43
View File
@@ -0,0 +1,43 @@
// ISC, Copyright 2017 Jaco Greeff
const { promisify } = require('./index');
describe('promisify', () => {
it('requires a function to promisify', () => {
expect(
() => promisify()
).toThrow(/Expected function input/);
});
it('handles functions with no parameters (resolve)', () => {
const fn = (cb) => cb(null, [true, 'test', 1]);
return promisify(fn).then((result) => {
expect(result).toEqual([true, 'test', 1]);
});
});
it('handles functions with no parameters (reject)', () => {
const fn = (cb) => cb(new Error('test reject'));
return promisify(fn).catch((error) => {
expect(error.message).toEqual('test reject');
});
});
it('handles functions with parameters (resolve)', () => {
const fn = (a, b, c, cb) => cb(null, [a, b, c]);
return promisify(fn, 2, false, null).then((result) => {
expect(result).toEqual([2, false, null]);
});
});
it('handles functions with parameters (reject)', () => {
const fn = (a, b, c, cb) => cb(new Error(`test reject: ${a},${b},${c}`));
return promisify(fn, 3, 'string', true).catch((error) => {
expect(error.message).toEqual('test reject: 3,string,true');
});
});
});