synchronized-promise
Advanced tools
Comparing version
/* 10 seconds */ | ||
const DEFAULT_TIMEOUTS = 10 * 1000; | ||
var DEFAULT_TIMEOUTS = 10 * 1000; | ||
/** | ||
* | ||
* @param {Function} func | ||
* @param {any} [options={}] | ||
* @param {Function} func Promise-base function that want to be transformed | ||
* @param {Object} options Additional options | ||
* @param {number} options.timeouts Function call timeouts | ||
* @returns {Function} | ||
*/ | ||
function syncPromise(func, options = {}) { | ||
return () => { | ||
let promiseError, promiseValue; | ||
let hasError = false; | ||
let args = Array.isArray(options.args) ? options.args : [options.args]; | ||
const timeouts = options.timeouts || DEFAULT_TIMEOUTS; | ||
func.apply(this, args).then(value => { | ||
function sp(func) { | ||
var _this = this; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return function () { | ||
var promiseError, promiseValue; | ||
var hasError = false; | ||
var timeouts = options.timeouts || DEFAULT_TIMEOUTS; | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
func.apply(_this, args).then(function (value) { | ||
promiseValue = value; | ||
}).catch(e => { | ||
}).catch(function (e) { | ||
promiseError = e; | ||
hasError = true; | ||
}); | ||
const waitUntil = new Date(new Date().getTime() + timeouts); | ||
var waitUntil = new Date(new Date().getTime() + timeouts); | ||
@@ -40,2 +47,2 @@ while (waitUntil > new Date() && typeof promiseError === 'undefined') { | ||
module.exports = syncPromise; | ||
module.exports = sp; |
@@ -5,12 +5,11 @@ /* 10 seconds */ | ||
/** | ||
* | ||
* @param {Function} func | ||
* @param {any} [options={}] | ||
* @param {Function} func Promise-base function that want to be transformed | ||
* @param {Object} options Additional options | ||
* @param {number} options.timeouts Function call timeouts | ||
* @returns {Function} | ||
*/ | ||
function syncPromise (func, options = {}) { | ||
return () => { | ||
function sp (func, options = {}) { | ||
return (...args) => { | ||
let promiseError, promiseValue | ||
let hasError = false | ||
let args = Array.isArray(options.args) ? options.args : [options.args] | ||
const timeouts = options.timeouts || DEFAULT_TIMEOUTS | ||
@@ -41,2 +40,2 @@ | ||
module.exports = syncPromise | ||
module.exports = sp |
{ | ||
"name": "synchronized-promise", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Turn ES6 Promise into synchronize function call, a simple wrapper of deasync package", | ||
@@ -9,3 +9,3 @@ "main": "dist/index.js", | ||
"build": "babel lib -d dist", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "ava" | ||
}, | ||
@@ -21,2 +21,5 @@ "repository": { | ||
], | ||
"engines": { | ||
"node": ">=4.2.0" | ||
}, | ||
"author": "Yukai Huang <yukaihuangtw@gmail.com> (https://yukaii.tw)", | ||
@@ -32,6 +35,7 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@babel/cli": "^7.0.0-beta.39", | ||
"@babel/core": "^7.0.0-beta.39", | ||
"@babel/preset-env": "^7.0.0-beta.39", | ||
"@babel/cli": "^7.0.0-beta.39" | ||
"ava": "^0.25.0" | ||
} | ||
} |
# synchronized-promise | ||
[](https://badge.fury.io/js/synchronized-promise) | ||
[](https://travis-ci.org/Yukaii/synchronized-promise) | ||
Turn ES6 Promise into synchronize function call, a simple wrapper of deasync package | ||
@@ -4,0 +7,0 @@ |
Sorry, the diff of this file is not supported yet
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
6297
78.44%8
33.33%107
55.07%1
-50%35
9.38%4
33.33%