mailtrain/test/e2e/lib/semaphore.js

36 lines
593 B
JavaScript
Raw Normal View History

2017-05-27 11:28:40 +00:00
'use strict';
const Promise = require('bluebird');
class Semaphore {
constructor() {
this.counter = 0;
}
enter() {
this.counter++;
}
exit() {
this.counter--;
}
async waitForEmpty() {
const self = this;
function wait(resolve) {
2017-05-27 12:24:08 +00:00
if (self.counter === 0) {
2017-05-27 11:28:40 +00:00
resolve();
} else {
setTimeout(wait, 500, resolve);
}
}
return new Promise(resolve => {
setTimeout(wait, 500, resolve);
2017-05-27 12:24:08 +00:00
});
2017-05-27 11:28:40 +00:00
}
}
module.exports = Semaphore;