Moving semicolon, changing example in promisify.md
This commit is contained in:
@ -1,15 +1,14 @@
|
|||||||
### Promisify
|
### Promisify
|
||||||
|
|
||||||
Creates a promisified version of the given callback-style function. In Node 8+, you
|
Use currying to return a function returning a Promise that calls the original function. Use the rest operator to pass in all the parameters. In Node 8+, you can use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original)
|
||||||
can use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original)
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const promisify = func =>
|
const promisify = func =>
|
||||||
(...args) =>
|
(...args) =>
|
||||||
new Promise((resolve, reject) =>
|
new Promise((resolve, reject) =>
|
||||||
func(...args, (err, result) =>
|
func(...args, (err, result) =>
|
||||||
err ? reject(err) : resolve(result));
|
err ? reject(err) : resolve(result))
|
||||||
)
|
);
|
||||||
// const stat = promisify(fs.stat)
|
// const delay = promisify((d, cb) => setTimeout(cb, d))
|
||||||
// stat('foo.txt') -> Promise resolves if `foo.txt` exists, otherwise rejects
|
// delay(2000).then(() => console.log('Hi!')) -> Promise resolves after 2s
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user