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!`
```