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
$ npm install wrap-as-async --save
Synopsis
var wrap = ; // 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 = ; // The return value of function `wrapped` indicates// whether the original function is asynchronous,// which might be useful.var is_async = ;
Wrap a sync method into async
var wrapped = ; var is_async = ; is_async; // false
this.async()
Wrap an async function using var wrapped = ; var is_async = ; is_async; // true ;
this
object
Handles 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.
{ thisdecorate = ;} myClassprototypemethod = ;
And also could assign this
object by using call
:
; ;
done
result
Multiple arguments and 1 2 { // result1 -> 2 // result2 -> 3};
Synchronous and asynchronous Methods
{ 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.
{ var done = this; ;}
You could use this.async()
to turn the method into an asynchonous method, and this.async
will return the callback function.
Promise
support
1 { // result -> 2}
License
MIT