# wrap-as-async

> Utility method to wrap a function into an asynchronous method using the common this.async() style.

Latest version **1.3.1** (published 2016-08-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install wrap-as-async
pnpm add wrap-as-async
yarn add wrap-as-async
bun add wrap-as-async
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2016-08-30 |
| First published | 2015-10-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | kaelzhang |
| Maintainers | kael |
| Keywords | wrap-as-async, wrap, async, this.async(), run, run-async |

## Links

- npm: https://www.npmjs.com/package/wrap-as-async
- Repository: https://github.com/kaelzhang/wrap-as-async
- Homepage: https://github.com/kaelzhang/wrap-as-async#readme
- Issues: https://github.com/kaelzhang/wrap-as-async/issues
- npm.io page: https://npm.io/package/wrap-as-async

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 1.3.1 (latest) — 2016-08-30
- 1.3.0 — 2016-08-30
- 1.2.2 — 2015-12-04
- 1.2.1 — 2015-11-21
- 1.2.0 — 2015-11-21
- 1.1.8 — 2015-11-21
- 1.1.7 — 2015-11-08
- 1.1.6 — 2015-11-08
- 1.1.5 — 2015-11-08
- 1.1.4 — 2015-10-28
- 1.1.3 — 2015-10-28
- 1.1.2 — 2015-10-28
- 1.1.1 — 2015-10-28
- 1.1.0 — 2015-10-28
- 1.0.5 — 2015-10-27
- … 5 more at https://npm.io/package/wrap-as-async/versions

## README

[![Build Status](https://travis-ci.org/kaelzhang/wrap-as-async.svg?branch=master)](https://travis-ci.org/kaelzhang/wrap-as-async)
<!-- optional npm version
[![NPM version](https://badge.fury.io/js/wrap-as-async.svg)](http://badge.fury.io/js/wrap-as-async)
-->
<!-- optional npm downloads
[![npm module downloads per month](http://img.shields.io/npm/dm/wrap-as-async.svg)](https://www.npmjs.org/package/wrap-as-async)
-->
<!-- optional dependency status
[![Dependency Status](https://david-dm.org/kaelzhang/wrap-as-async.svg)](https://david-dm.org/kaelzhang/wrap-as-async)
-->

# wrap-as-async

Utility method to
- wrap a function which returns a `Promise` into a normal asynchronous function.
- wrap a function into an asynchronous method using the common `this.async()` style, with browser compatibility.

## Install

```sh
$ npm install wrap-as-async --save
```

## Synopsis

```js
var wrap = require('wrap-as-async');

// Wrap a synchronous function into an asynchronous one.
// Or wrap a function that using the `this.async()` style
//   into a normal asynchronous function.
// `wrapped` is an asynchronous function.
var wrapped = wrap(fn);

// The return value of function `wrapped` indicates
//   whether the original function is asynchronous,
//   which might be useful.
var is_async = wrapped(args, function(err, result){
  // The callback of either sync or async function
  //   will always has the `err` as the first argument.
});
```

#### Wrap a sync method into async

```js
var wrapped = wrap(function (n){
  return n + 1;
});

var is_async = wrapped(1, function(err, result){
  console.log(err); // null
  console.log(result); // 2
});

is_async; // false
```

#### Wrap an async function using `this.async()`

```js
var wrapped = wrap(function(n){
  var done = this.async();
  setTimeout(function(){
    if (n < 0) {
      return done(new Error('n should not less than 0'));
    }
    done(null, n + 1);
  }, 10)
});

var is_async = wrapped(1, function(err, result){
  console.log(err); // null
  console.log(result); // 2
});

is_async; // true

wrapped(-1, function(err){
  console.log(err); // Error
});
```

#### Handles `this` object

`wrap-as-async` handles `this` object, so the `wrap()`ped function could be assigned onto function prototypes, instances or singletons, acting like  a decorator(such as python decorators), which will be really helpful.

```js
function myClass (decorate) {
  this.decorate = wrap(decorate);
}

myClass.prototype.method = wrap(method);
```

And also could assign `this` object by using `call`:

```js
wrap(function(n){
  return n + this.base

}).call({
  base: 2
}, 1, function(err, result){
  // result -> 3
});


wrap(function(n){
  // You could still use `this.async()` even with `call`
  var done = this.async();
  var base = this.base;
  setTimeout(function(){
    done(null, n + base)
  }, 10)

}).call({
  base: 2
}, 1, function(err, result){
  // result -> 3
});
```

#### Multiple arguments and `done` result

```js
wrap(function(n, m){
  var done = this.async();
  done(null, n + 1, m + 1);

})(1, 2, function(err, result1, result2){
  // result1 -> 2
  // result2 -> 3
});
```

## Synchronous and asynchronous Methods

```js
function sync_method (arg...){
  return something
}
```

If the method to be wrapped returns an instance of `Error`, it will be treated as a failure instead, or the `returnValue` will be the result.

```js
function async_method (arg...) {
  var done = this.async();
  someAsyncProcess(function(...){
    ...
    done(err, result);
  });
}
```
You could use `this.async()` to turn the method into an asynchonous method, and `this.async` will return the callback function.


## `Promise` support

```js
wrap(function(n){
  return Promise.resolve(n + 1)
})(1, (err, result) => {
  // result -> 2
})
```

## License

MIT

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