Skip to content

Commit

Permalink
busywait-7 (#8)
Browse files Browse the repository at this point in the history
* #7 - protect the options argumnet and refrain form using the async reserved keyword

* #7 - protect the options argumnet and refrain form using the async reserved keyword
  • Loading branch information
regevbr authored Nov 10, 2017
1 parent 2330374 commit 33c5935
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
27 changes: 10 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
"use strict";

module.exports = {
sync: sync,
async: async
sync: _sync,
async: _async
};

function sync(syncCheckFn, options) {
function _sync(syncCheckFn, options) {
return _busywait(syncCheckFn, options, true);
}

function async(asyncCheckFn, options) {
function _async(asyncCheckFn, options) {
return _busywait(asyncCheckFn, options, false);
}

function _busywait(checkFn, options, wrapCheckFun) {
function _busywait(checkFn, _options, wrapCheckFun) {
const options = Object.assign({}, _options);
return _checkArgs(checkFn, options)
.then(function () {
let asyncCheckFn = checkFn;
if (wrapCheckFun) {
asyncCheckFn = _wrapSyncMethod(checkFn);
}
return _eventLoop(asyncCheckFn, options);
return _eventLoop(wrapCheckFun ? _wrapSyncMethod(checkFn) : checkFn, options);
});
}

Expand All @@ -39,18 +36,14 @@ function _checkArgs(checkFn, options) {

function _wrapSyncMethod(checkFn) {
return function (iteration) {
return new Promise(function (resolve, reject) {
if (checkFn(iteration)) {
return resolve(true);
} else {
return reject();
}
return new Promise((resolve, reject) => {
return checkFn(iteration) ? resolve(true) : reject();
});
};
}

function _eventLoop(asyncCheckFn, options) {
return new Promise(function busyWait(resolve, reject) {
return new Promise((resolve, reject) => {
let iteration = 0;
const iterationCheck = function () {
iteration++;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "busywait",
"version": "1.0.10",
"version": "1.1.0",
"description": "Async busy wait",
"main": "./lib/index.js",
"scripts": {
Expand Down

0 comments on commit 33c5935

Please sign in to comment.