Comparing version 0.1.13 to 0.1.14
121
index.js
@@ -5,73 +5,70 @@ /*! | ||
* | ||
* Copyright 2014-2015 Abbr | ||
* Copyright 2014-present Abbr | ||
* Released under the MIT license | ||
*/ | ||
(function () { | ||
var fs = require('fs'), | ||
path = require('path'), | ||
binding; | ||
// Seed random numbers [gh-82] if on Windows. See https://github.com/laverdet/node-fibers/issues/82 | ||
if(process.platform === 'win32') Math.random(); | ||
// Look for binary for this platform | ||
var nodeV = 'node-' + /[0-9]+\.[0-9]+/.exec(process.versions.node)[0]; | ||
var nodeVM = 'node-' + /[0-9]+/.exec(process.versions.node)[0]; | ||
var modPath = path.join(__dirname, 'bin', process.platform + '-' + process.arch + '-' + nodeV, 'deasync'); | ||
var fs = require('fs'), | ||
path = require('path'), | ||
binding | ||
// Seed random numbers [gh-82] if on Windows. See https://github.com/laverdet/node-fibers/issues/82 | ||
if (process.platform === 'win32') Math.random() | ||
// Look for binary for this platform | ||
var nodeV = 'node-' + /[0-9]+\.[0-9]+/.exec(process.versions.node)[0] | ||
var nodeVM = 'node-' + /[0-9]+/.exec(process.versions.node)[0] | ||
var modPath = path.join(__dirname, 'bin', process.platform + '-' + process.arch + '-' + nodeV, 'deasync') | ||
try { | ||
try { | ||
try{ | ||
fs.statSync(modPath + '.node'); | ||
} | ||
catch(ex){ | ||
modPath = path.join(__dirname, 'bin', process.platform + '-' + process.arch + '-' + nodeVM, 'deasync'); | ||
fs.statSync(modPath + '.node'); | ||
} | ||
binding = require(modPath); | ||
fs.statSync(modPath + '.node') | ||
} catch (ex) { | ||
modPath = path.join(__dirname, 'bin', process.platform + '-' + process.arch + '-' + nodeVM, 'deasync') | ||
fs.statSync(modPath + '.node') | ||
} | ||
catch (ex) { | ||
binding = require('bindings')('deasync'); | ||
} | ||
function deasync(fn) { | ||
return function() { | ||
var done = false; | ||
var args = Array.prototype.slice.apply(arguments).concat(cb); | ||
var err; | ||
var res; | ||
binding = require(modPath) | ||
} catch (ex) { | ||
binding = require('bindings')('deasync') | ||
} | ||
fn.apply(this, args); | ||
module.exports.loopWhile(function(){return !done;}); | ||
if (err) | ||
throw err; | ||
function deasync(fn) { | ||
return function () { | ||
var done = false | ||
var args = Array.prototype.slice.apply(arguments).concat(cb) | ||
var err | ||
var res | ||
return res; | ||
fn.apply(this, args) | ||
module.exports.loopWhile(function () { | ||
return !done | ||
}) | ||
if (err) | ||
throw err | ||
function cb(e, r) { | ||
err = e; | ||
res = r; | ||
done = true; | ||
} | ||
return res | ||
function cb(e, r) { | ||
err = e | ||
res = r | ||
done = true | ||
} | ||
} | ||
module.exports = deasync; | ||
module.exports.sleep = deasync(function(timeout, done) { | ||
setTimeout(done, timeout); | ||
}); | ||
module.exports.runLoopOnce = function(){ | ||
process._tickCallback(); | ||
binding.run(); | ||
}; | ||
module.exports.loopWhile = function(pred){ | ||
while(pred()){ | ||
process._tickCallback(); | ||
if(pred()) binding.run(); | ||
} | ||
}; | ||
} | ||
}()); | ||
module.exports = deasync | ||
module.exports.sleep = deasync(function (timeout, done) { | ||
setTimeout(done, timeout) | ||
}) | ||
module.exports.runLoopOnce = function () { | ||
process._tickCallback() | ||
binding.run() | ||
} | ||
module.exports.loopWhile = function (pred) { | ||
while (pred()) { | ||
process._tickCallback() | ||
if (pred()) binding.run() | ||
} | ||
} |
{ | ||
"name": "deasync", | ||
"version": "0.1.13", | ||
"version": "0.1.14", | ||
"description": "Turns async function into sync via JavaScript wrapper of Node event loop", | ||
@@ -15,4 +15,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"bindings": "~1.2.1", | ||
"nan": "^2.0.7" | ||
"node-addon-api": "^1.6.0", | ||
"bindings": "~1.2.1" | ||
}, | ||
@@ -19,0 +19,0 @@ "repository": { |
@@ -26,3 +26,3 @@ DeAsync.js | ||
* Generic wrapper of async function with standard API signature `function(p1,...pn,function cb(error,result){})`. Returns `result` and throws `error` as exception if not null: | ||
* Generic wrapper of async function with conventional API signature `function(p1,...pn,function cb(error,result){})`. Returns `result` and throws `error` as exception if not null: | ||
@@ -44,3 +44,3 @@ ```javascript | ||
* For async function with non-standard API, for instance `function asyncFunction(p1,function cb(res){})`, use `loopWhile(predicateFunc)` where `predicateFunc` is a function that returns boolean loop condition | ||
* For async function with unconventional API, for instance `function asyncFunction(p1,function cb(res){})`, use `loopWhile(predicateFunc)` where `predicateFunc` is a function that returns boolean loop condition | ||
@@ -47,0 +47,0 @@ ```javascript |
40
test.js
@@ -1,31 +0,35 @@ | ||
var deasync = require('./index.js'); | ||
var cp = require('child_process'); | ||
var http = require('http'); | ||
var deasync = require('./index.js') | ||
var cp = require('child_process') | ||
var https = require('https') | ||
var exec = deasync(cp.exec); | ||
var exec = deasync(cp.exec) | ||
var sleep = deasync(function (timeout, done) { | ||
setTimeout(done, timeout); | ||
}); | ||
setTimeout(done, timeout) | ||
}) | ||
var request = deasync(function (url, done) { | ||
http.get(url, function (res) { | ||
res.on('error', done); | ||
https.get(url, function (res) { | ||
res.on('error', done) | ||
res.setEncoding('utf8'); | ||
res.setEncoding('utf8') | ||
var result = '' | ||
res.on('data', function (data) { result += data; }); | ||
res.on('end', function () { done(null, result); }); | ||
}).on('error', done); | ||
}); | ||
res.on('data', function (data) { | ||
result += data | ||
}) | ||
res.on('end', function () { | ||
done(null, result) | ||
}) | ||
}).on('error', done) | ||
}) | ||
setTimeout(function () { | ||
console.log('async'); | ||
}, 1000); | ||
console.log('async') | ||
}, 1000) | ||
console.log(exec('ls -la')); | ||
sleep(2000); | ||
console.log(request('http://nodejs.org')); | ||
console.log(exec('ls -la')) | ||
sleep(2000) | ||
console.log(request('https://nodejs.org/en/')) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
2420530
66
222
+ Addednode-addon-api@^1.6.0
+ Addednode-addon-api@1.7.2(transitive)
- Removednan@^2.0.7
- Removednan@2.22.0(transitive)