Skip to content

Commit

Permalink
Remove unnecssary closures from sleep and defer
Browse files Browse the repository at this point in the history
  • Loading branch information
raksoras committed Oct 4, 2017
1 parent 6479e8f commit 9b6cd5d
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lib/koroutine.js
Expand Up @@ -225,10 +225,10 @@ exports.join = function* (...futures) {
return errorCount;
};

function assertCalledFromKoroutine () {
function assertCalledFromKoroutine (name) {
const resume = _resume;
if (!resume) {
throw new Error('This call must be called from within a running koroutine');
throw new Error(name + ' must be called from within an active koroutine');
}
return resume;
}
Expand All @@ -238,18 +238,14 @@ function assertCalledFromKoroutine () {
* @param ms {Number} Number of milliseconds to sleep
*/
exports.sleep = function (timeout) {
const resume = assertCalledFromKoroutine();
setTimeout(function () {
resume();
}, timeout);
const resume = assertCalledFromKoroutine('sleep');
setTimeout(resume, timeout);
};

/**
* Akin to thread.yield()
*/
exports.defer = function () {
const resume = assertCalledFromKoroutine();
setImmediate(function () {
resume();
});
const resume = assertCalledFromKoroutine('defer');
setImmediate(resume);
};

0 comments on commit 9b6cd5d

Please sign in to comment.