# makepromise

> Make a Promise from a function with a callback and preserve its error stack.

Latest version **3.2.0** (published 2020-02-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install makepromise
pnpm add makepromise
yarn add makepromise
bun add makepromise
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2020-02-03 |
| First published | 2017-05-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 12.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Anton |
| Maintainers | zvr |
| Keywords | promise, async, await, ecmascript, es6, ecma, promisify, node, callback, cb, error, stack |

## Links

- npm: https://www.npmjs.com/package/makepromise
- Repository: https://github.com/artdecocode/makepromise
- Homepage: https://www.artd.eco/
- Issues: https://github.com/artdecocode/makepromise/issues
- npm.io page: https://npm.io/package/makepromise

## Dependencies (1)

- [erotic](https://npm.io/package/erotic.md) ^2.1.0

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 3.2.0 (latest) — 2020-02-03
- 4.0.0 — 2020-02-03
- 3.1.1 — 2020-02-03
- 3.1.0 — 2019-04-10
- 3.0.3 — 2019-04-02
- 3.0.2 — 2019-01-14
- 3.0.1 — 2018-08-31
- 3.0.0 — 2018-08-31
- 2.0.0 — 2018-05-12
- 1.1.1 — 2017-12-29
- 1.1.0 — 2017-12-29
- 1.0.0 — 2017-05-20

## README

# makepromise

[![npm version](https://badge.fury.io/js/makepromise.svg)](https://www.npmjs.com/package/makepromise)

`makepromise` can be used to get a _Promise_ from a function with a callback. It will also make sure that the error stack starts at the line where the `makepromise` was called.

```
yarn add -E makepromise
```

## Table Of Contents

- [Table Of Contents](#table-of-contents)
- [API](#api)
  * [`async makePromise(fn: function(...args, cb), args: (*[]|*))`](#async-makepromisefn-functionargs-cbargs--void)
  * [`async makePromise(fn: function(...args, cb), args: (*[]|*), resolveValue: *)`](#async-makepromisefn-functionargs-cbargs-resolvevalue--void)
  * [Binding Methods](#binding-methods)
- [Error Stack](#error-stack)
- [Copyright](#copyright)

## API

The package exports a default `makepromise` function.

```js
import makePromise from 'makepromise'
```

### <code>async <ins>makePromise</ins>(</code><sub><br/>&nbsp;&nbsp;`fn: function(...args, cb),`<br/>&nbsp;&nbsp;`args: (*[]|*),`<br/></sub><code>): <i>void</i></code>

Create a promise from a function which accepts a callback as the last argument, and where the callback will be called with 2 arguments: `error` and `result`. The arguments must be passed either as an array, or a single value.

The example below shows how to use `makePromise` with an array of arguments (promisified `truncate`) and a single argument (promisified `unlink`). The context of the example is 2 methods from a lib to create a temp file, and read data from a file.

```js
import { truncate, unlink, existsSync } from 'fs'
import makePromise from 'makepromise'
import { createTempFile, readFile } from './lib'

(async () => {
  try {
    // 0. SETUP: create a temp file.
    const path = await createTempFile('hello-world')
    const data = readFile(path)
    console.log('Created temp file %s', path)
    console.log('Exists: %s', existsSync(path))
    console.log('Content: "%s"', data)

    // 1. TRUNCATE to 5 characters.
    await makePromise(truncate, [path, 5])
    const data2 = await readFile(path)
    console.log('Content: "%s"', data2)

    // 2. ERASE the temp file.
    await makePromise(unlink, path)
    console.log('Exists: %s', existsSync(path))
  } catch (err) {
    console.log(err)
  }
})()
```

```
Created temp file example/temp.data
Exists: true
Content: "hello-world"
Content: "hello"
Exists: false
```

### <code>async <ins>makePromise</ins>(</code><sub><br/>&nbsp;&nbsp;`fn: function(...args, cb),`<br/>&nbsp;&nbsp;`args: (*[]|*),`<br/>&nbsp;&nbsp;`resolveValue: *,`<br/></sub><code>): <i>void</i></code>

When `resolveValue` is passed as the last argument to the `makePromise` function, the returned promise will be forced to resolve with it.

```js
import { unlink } from 'fs'
import makePromise from 'makepromise'
import { createTempFile } from './lib'

(async () => {
  try {
    // 0. SETUP: create a temp file.
    const path = await createTempFile()

    // 1. UNLINK and return the path to the temp file.
    const erasedPath = await makePromise(unlink, path, path)
    console.log('Erased: %s', erasedPath)
  } catch (err) {
    console.log(err)
  }
})()
```

```
Erased: example/temp.data
```

### Binding Methods

Sometimes, it is important to bind methods of instances to their contexts, otherwise they will loose access to `this`.

For example. when closing a _Writable_ stream, its `close` method must be bound to its instance.

```js
import { createWriteStream, unlink } from 'fs'
import makePromise from 'makepromise'
import { createTempFile, readFile } from './lib'

(async () => {
  try {
    // 0. SETUP: create a temp file.
    const path = await createTempFile()

    // 1. CREATE a write stream, and end it with data.
    const ws = createWriteStream(path)
    await makePromise(ws.end.bind(ws), 'example-data')

    // 2. CHECK that data has been written.
    const data = await readFile(path)
    console.log('Read file: "%s"', data)

    // 3. TEAR-DOWN: remove file.
    await makePromise(unlink, path)
  } catch (err) {
    console.log(err)
  }
})()
```

```
Read file: "example-data"
```

## Error Stack

This modules will make sure that errors are updated to include the stack trace of when `makePromise` was called, rather than have the Node's internal error stack or no stack at all.

```js
import { unlink } from 'fs'
import makePromise from 'makepromise'

(async () => {
  try {
    await makePromise(unlink, 'error-test-file')
  } catch ({ stack }) {
    console.log(stack)
  }
})()
```

```
Error: ENOENT: no such file or directory, unlink 'error-test-file'
    at /Users/anton/adc/makepromise/example/error-stack.js:6:11
    at Object.<anonymous> (/Users/anton/adc/makepromise/example/error-stack.js:10:3)
    at Module.p._compile (/Users/anton/adc/makepromise/node_modules/alamode/compile/depack.js:49:18)
    at Object.k.(anonymous function).y._extensions.(anonymous function) [as .js] (/Users/anton/adc/makepromise/node_modules/alamode/compile/depack.js:51:7)
```

Without this functionality, the error stack would not appear.

```js
import { unlink } from 'fs'

(async () => {
  try {
    await new Promise((r, j) => {
      unlink('error-test-file', (err) => {
        if (err) return j(err)
        return r()
      })
    })
  } catch ({ stack }) {
    console.log(stack)
  }
})()
```

```
Error: ENOENT: no such file or directory, unlink 'error-test-file'
```



## Copyright

(c) [Art Deco][1] 2019

[1]: https://artd.eco

---
_Source: https://npm.io/package/makepromise · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
