Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-await

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-await - npm Package Compare versions

Comparing version 0.1.39 to 0.1.40

aa.js

226

aa02.js

@@ -12,2 +12,6 @@ // Callbacks vs Coroutines

var Promise = require('promise-thunk');
var isPromise = Promise.isPromise;
var isIterator = Promise.isIterator;
var isIterable = Promise.isIterable;
var makeArrayFromIterator = Promise.makeArrayFromIterator;

@@ -145,3 +149,4 @@ var slice = [].slice;

function funcb(fun, cb) { fun.aa$callback(cb); }
function anycb(val, cb) { typecbs[typeof val](val, cb); }
//function anycb(val, cb) { typecbs[typeof val](val, cb); }
function anycb(val, cb) { val && typeof val.then === 'function' ? promisecb(val, cb) : typecbs[typeof val](val, cb); }
function clscb(val, cb) { val ? ctorcb(val.constructor.displayName || val.constructor.name || '$', val, cb) : nextTick(cb, null, val); }

@@ -316,2 +321,3 @@ function ctorcb(name, val, cb) { (ctorcbs[name] ? ctorcbs[name] : ctorcbs.$)(val, cb); }

function aa(val) {
if (arguments.length === 0) return Channel();
if (arguments.length <= 1 && typeof val === 'function' && !isGeneratorFunction(val)) return aa.promisify(val);

@@ -394,2 +400,3 @@ var resolve, reject, callback, result;

return chan;
function channel(err, val) {

@@ -429,212 +436,23 @@ if (typeof val === 'function')

// aa.thunkify(fn)
setValue(aa, 'thunkify', thunkify);
function thunkify(fn, options) {
// thunkify(object target, string method, [object options]) : undefined
if (fn && typeof fn === 'object' && options && typeof options === 'string') {
var object = fn, method = options, options = arguments[2];
var suffix = options && typeof options === 'string' ? options :
options && typeof options.suffix === 'string' ? options.suffix :
options && typeof options.postfix === 'string' ? options.postfix : 'Async';
var methodAsyncCached = method + suffix + 'Cached';
Object.defineProperty(object, method + suffix, {
get: function () {
return this.hasOwnProperty(methodAsyncCached) &&
typeof this[methodAsyncCached] === 'function' ? this[methodAsyncCached] :
(setValue(this, methodAsyncCached, thunkify(this, this[method])), this[methodAsyncCached]);
},
configurable: true
});
return;
}
// thunkify([object ctx,] function fn) : function
var ctx = typeof this !== 'function' ? this : undefined;
if (typeof options === 'function') ctx = fn, fn = options, options = arguments[2];
if (options && options.context) ctx = options.context;
if (typeof fn !== 'function')
throw new TypeError('thunkify: argument must be a function');
// thunkified
thunkified.thunkified = true;
return thunkified;
function thunkified() {
var result, callbacks = [], unhandled;
arguments[arguments.length++] = function callback(err, val) {
if (result) {
if (err)
console.error(COLOR_ERROR + 'Unhandled callback error: ' + err2str(err) + COLOR_NORMAL);
return;
}
result = arguments;
if (callbacks.length === 0 && err instanceof Error)
unhandled = true,
console.error(COLOR_ERROR + 'Unhandled callback error: ' + err2str(err) + COLOR_NORMAL);
for (var i = 0, n = callbacks.length; i < n; ++i)
fire(callbacks[i]);
callbacks = [];
};
fn.apply(ctx, arguments);
// thunk
return function thunk(cb) {
if (typeof cb !== 'function')
throw new TypeError('argument must be a function');
if (unhandled)
unhandled = false,
console.error(COLOR_ERROR + 'Unhandled callback error handled: ' + err2str(result[0]) + COLOR_NORMAL);
if (result) return fire(cb);
callbacks.push(cb);
};
// fire
function fire(cb) {
var err = result[0], val = result[1];
try {
return err instanceof Error || result.length === cb.length ? cb.apply(ctx, result) :
// normal node style callback
result.length === 2 ? cb.call(ctx, err, val) :
// fs.exists like callback, arguments[0] is value
result.length === 1 ? cb.call(ctx, null, result[0]) :
// unknown callback
result.length === 0 ? cb.call(ctx) :
// child_process.exec like callback
cb.call(ctx, null, slice.call(result, err == null ? 1 : 0));
} catch (e) { cb.call(ctx, e); }
} // fire
}; // thunkified
} // thunkify
// aa.promisify(fn)
setValue(aa, 'wrap', promisify);
setValue(aa, 'promisify', promisify);
function promisify(fn, options) {
// promisify(object target, string method, [object options]) : undefined
if (fn && typeof fn === 'object' && options && typeof options === 'string') {
var object = fn, method = options, options = arguments[2];
var suffix = options && typeof options === 'string' ? options :
options && typeof options.suffix === 'string' ? options.suffix :
options && typeof options.postfix === 'string' ? options.postfix : 'Async';
var methodAsyncCached = method + suffix + 'Cached';
Object.defineProperty(object, method + suffix, {
get: function () {
return this.hasOwnProperty(methodAsyncCached) &&
typeof this[methodAsyncCached] === 'function' ? this[methodAsyncCached] :
(setValue(this, methodAsyncCached, promisify(this, this[method])), this[methodAsyncCached]);
},
configurable: true
});
return;
}
// promisify([object ctx,] function fn) : function
var ctx = typeof this !== 'function' ? this : undefined;
if (typeof options === 'function') ctx = fn, fn = options, options = arguments[2];
if (options && options.context) ctx = options.context;
if (typeof fn !== 'function')
throw new TypeError('promisify: argument must be a function');
// promisified
promisified.promisified = true;
return promisified;
function promisified() {
var args = arguments;
return new Promise(function (res, rej) {
args[args.length++] = function callback(err, val) {
try {
return err instanceof Error ? rej(err) :
// normal node style callback
arguments.length === 2 ? (err ? rej(err) : res(val)) :
// fs.exists like callback, arguments[0] is value
arguments.length === 1 ? res(arguments[0]) :
// unknown callback
arguments.length === 0 ? res() :
// child_process.exec like callback
res(slice.call(arguments, err == null ? 1 : 0));
} catch (e) { rej(e); }
};
fn.apply(ctx, args);
});
// aa.callback(gtor)
setValue(aa, 'callback', callback);
function callback(gtor) {
return function () {
return aa(gtor.apply(this, arguments));
};
} // promisify
// aa.thunkifyAll(object, options)
setValue(aa, 'thunkifyAll', thunkifyAll);
function thunkifyAll(object, options) {
var keys = Object_getOwnPropertyNames(object);
keys.forEach(function (method) {
if (typeof object[method] === 'function' &&
!object[method].promisified &&
!object[method].thunkified)
thunkify(object, method, options);
});
return object;
}
// aa.promisifyAll(object, options)
setValue(aa, 'promisifyAll', promisifyAll);
function promisifyAll(object, options) {
var keys = Object_getOwnPropertyNames(object);
keys.forEach(function (method) {
if (typeof object[method] === 'function' &&
!object[method].promisified &&
!object[method].thunkified)
promisify(object, method, options);
});
return object;
}
// isPromise(promise)
setValue(aa, 'isPromise', isPromise);
function isPromise(promise) {
return !!promise && typeof promise.then === 'function';
}
// isIterator(iter)
setValue(aa, 'isIterator', isIterator);
function isIterator(iter) {
return !!iter && (typeof iter.next === 'function' || isIterable(iter));
}
// isIterable(iter)
setValue(aa, 'isIterable', isIterable);
function isIterable(iter) {
return !!iter && typeof Symbol === 'function' &&
!!Symbol.iterator && typeof iter[Symbol.iterator] === 'function';
}
// makeArrayFromIterator(iter or array)
setValue(aa, 'makeArrayFromIterator', makeArrayFromIterator);
function makeArrayFromIterator(iter) {
if (iter instanceof Array) return iter;
if (!isIterator(iter)) return [iter];
if (isIterable(iter)) iter = iter[Symbol.iterator]();
var array = [];
try {
for (;;) {
var val = iter.next();
if (val && val.hasOwnProperty('done') && val.done) return array;
if (val && val.hasOwnProperty('value')) val = val.value;
array.push(val);
}
} catch (error) {
return array;
}
} // makeArrayFromIterator
setValue(aa, 'Promise', Promise);
setValue(aa, 'PromiseThunk', Promise);
if (Object.getOwnPropertyNames)
Object.getOwnPropertyNames(PromiseThunk).forEach(function (prop) {
if (!aa.hasOwnProperty(prop))
setValue(aa, prop, PromiseThunk[prop]);
});
else
for (var prop in PromiseThunk)
if (!aa.hasOwnProperty(prop))
setValue(aa, prop, PromiseThunk[prop]);

@@ -641,0 +459,0 @@ setValue(aa, 'aa', aa);

{
"name": "async-await",
"version": "0.1.39",
"version": "0.1.40",
"description": "aa - async-await. co like library, go like channel, thunkify or promisify wrap package.",
"main": "aa.js",
"dependencies": {
"promise-thunk": ">=0.1.15"
"promise-thunk": ">=0.1.16"
},
"devDependencies": {
"co": "^4.6.0",
"co": ">=4.6.0",
"control-c": ">=0.0.9",
"promise-light": ">=0.1.10"
"promise-light": ">=0.1.13"
},

@@ -14,0 +14,0 @@ "keywords": [

@@ -241,2 +241,13 @@ [aa](https://www.npmjs.com/package/aa) - [async-await](https://www.npmjs.com/package/async-await)

### aa.callback(gtor) : returns callback function
`callback(gtor)` returns normal callback function
```js
http.createServer(aa.callback(function *(req, res) {
yield aa.wait(1000);
res.end('delayed hello');
})).listen(process.env.PORT || 8000);
```
EXAMPLES:

@@ -243,0 +254,0 @@ ----

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc