-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement counter #403
Labels
Comments
As I understood, I should make method |
What can you say about this implementation as example? I am not sure about callback const timeout = (timeout, count, fn, callback) => {
let finished = false;
let curcount = 0;
const timer = setTimeout(() => {
finished = true;
callback(new Error(FN_TIMEOUT));
}, timeout);
const sum = arg => {
curcount += arg;
if (count > curcount) return sum;
finished = true;
clearTimeout(timer);
callback(); // done
return null;
};
fn((...args) => {
if (!finished) {
clearTimeout(timer);
finished = true;
callback(...args);
}
});
return sum;
};
const data = metasync.timeout(
10000,
5,
callback => {
setTimeout(() => {
callback(null, 'someVal', 1, 2, 3, 4);
}, 1000);
},
(err, res, ...args) => {
if (err) console.error(err);
else {
console.log(res);
console.log(args);
}
}
);
data(1);
data(1);
data(6); // there callback will be called but without args
// or I can make something like this
function Count(count) {
this.count = count;
this.curcount = 0;
this.cb = null;
this.to = null;
}
Count.prototype.timeout = function(to) {
this.to = to;
return this;
};
Count.prototype.done = function(fn) {
this.cb = fn;
const sum = arg => {
this.curcount += arg;
if (this.count > this.curcount) return sum;
this.cb(/*...args*/);
return null;
};
return sum;
};
const count = arg => new Count(arg);
const data = count(10)
.timeout(2000)
.done(err => {
if (err) {
/*...*/
} else {
/*...*/
}
});
data(4);
data(-10);
data(500); // callback here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@nechaido please assign to somebody
The text was updated successfully, but these errors were encountered: