Files
30-seconds-of-code/snippets/promisify.md
Angelos Chalaris 202ef9d260 Update promisify.md
2020-01-13 10:06:24 +02:00

24 lines
681 B
Markdown

---
title: promisify
tags: function,promise,intermediate
---
Converts an asynchronous function to return a promise.
*In Node 8+, you can use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original)*
Use currying to return a function returning a `Promise` that calls the original function.
Use the `...rest` operator to pass in all the parameters.
```js
const promisify = func => (...args) =>
new Promise((resolve, reject) =>
func(...args, (err, result) => (err ? reject(err) : resolve(result)))
);
```
```js
const delay = promisify((d, cb) => setTimeout(cb, d));
delay(2000).then(() => console.log('Hi!')); // // Promise resolves after 2s
```