Skip to content

Commit cb1d10e

Browse files
author
Gyula Szalai
committed
Adds promise support. Fixes #26
1 parent 35078d5 commit cb1d10e

File tree

3 files changed

+187
-2
lines changed

3 files changed

+187
-2
lines changed

README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,32 @@ var opts = {
167167
'x-custom': 'headers'
168168
}
169169
};
170+
171+
// Usage with callback function
170172
waitOn(opts, function (err) {
171173
if (err) { return handleError(err); }
172174
// once here, all resources are available
173175
});
176+
177+
// Usage with promises
178+
waitOn(opts)
179+
.then(function () {
180+
// once here, all resources are available
181+
})
182+
.catch(function (err) {
183+
handleError(err);
184+
});
185+
186+
// Usage with async await
187+
try {
188+
await waitOn(opts);
189+
// once here, all resources are available
190+
} catch (err) {
191+
handleError(err);
192+
}
174193
```
175194

176-
waitOn(opts, cb) - function which triggers resource checks
195+
waitOn(opts, [cb]) - function which triggers resource checks
177196

178197
- opts.resources - array of string resources to wait for. prefix determines the type of resource with the default type of `file:`
179198
- opts.delay - optional initial delay in ms, default 0

lib/wait-on.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,26 @@ var WAIT_ON_SCHEMA = Joi.object().keys({
6464
@param opts.timeout integer - optional timeout in ms, default Infinity. Aborts with error.
6565
@param opts.verbose boolean - optional flag to turn on debug output
6666
@param opts.window integer - optional stabilization time in ms, default 750ms. Waits this amount of time for file sizes to stabilize or other resource availability to remain unchanged. If less than interval then will be reset to interval
67-
@param cb callback function with signature cb(err) - if err is provided then, resource checks did not succeed
67+
@param cb optional callback function with signature cb(err) - if err is provided then, resource checks did not succeed
68+
if not specified, wait-on will return a promise that will be rejected if resource checks did not succeed or resolved otherwise
6869
*/
6970
function waitOn(opts, cb) {
71+
if (cb !== undefined) {
72+
return waitOnImpl(opts, cb);
73+
} else {
74+
return new Promise(function (resolve, reject) {
75+
waitOnImpl(opts, function(err) {
76+
if (err) {
77+
reject(err);
78+
} else {
79+
resolve();
80+
}
81+
})
82+
});
83+
}
84+
}
85+
86+
function waitOnImpl(opts, cb) {
7087
var validResult = Joi.validate(opts, WAIT_ON_SCHEMA);
7188
if (validResult.error) { return cb(validResult.error); }
7289
opts = validResult.value; // use defaults

test/api.mocha.js

+149
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,156 @@ describe('api', function () {
506506
});
507507
});
508508

509+
describe('promise support', function () {
510+
it('should succeed when file resources are available', function (done) {
511+
temp.mkdir({}, function (err, dirPath) {
512+
var opts = {
513+
resources: [
514+
path.resolve(dirPath, 'foo'),
515+
path.resolve(dirPath, 'bar')
516+
]
517+
};
518+
fs.writeFileSync(opts.resources[0], 'data1');
519+
fs.writeFileSync(opts.resources[1], 'data2');
520+
waitOn(opts)
521+
.then(function () {
522+
done();
523+
})
524+
.catch(function (err) {
525+
done(err);
526+
});
527+
});
528+
});
529+
530+
it('should succeed when file resources are become available later', function (done) {
531+
temp.mkdir({}, function (err, dirPath) {
532+
var opts = {
533+
resources: [
534+
path.resolve(dirPath, 'foo'),
535+
path.resolve(dirPath, 'bar')
536+
]
537+
};
538+
539+
setTimeout(function () {
540+
fs.writeFile(opts.resources[0], 'data1', function () {});
541+
fs.writeFile(opts.resources[1], 'data2', function () {});
542+
}, 300);
543+
544+
waitOn(opts)
545+
.then(function () {
546+
done();
547+
})
548+
.catch(function (err) {
549+
done(err);
550+
});
551+
});
552+
});
509553

554+
it('should timeout when all resources are not available and timout option is specified', function (done) {
555+
temp.mkdir({}, function (err, dirPath) {
556+
var opts = {
557+
resources: [ path.resolve(dirPath, 'foo') ],
558+
timeout: 1000
559+
};
560+
waitOn(opts)
561+
.then(function () {
562+
done(new Error('Should not be resolved'));
563+
})
564+
.catch(function (err) {
565+
expect(err).toExist();
566+
done();
567+
});
568+
});
569+
});
570+
571+
it('should timeout when some resources are not available and timout option is specified', function (done) {
572+
temp.mkdir({}, function (err, dirPath) {
573+
var opts = {
574+
resources: [
575+
path.resolve(dirPath, 'foo'),
576+
path.resolve(dirPath, 'bar')
577+
],
578+
timeout: 1000
579+
};
580+
fs.writeFile(opts.resources[0], 'data', function () {});
581+
waitOn(opts)
582+
.then(function () {
583+
done(new Error('Should not be resolved'));
584+
})
585+
.catch(function (err) {
586+
expect(err).toExist();
587+
done();
588+
});
589+
});
590+
});
510591

592+
it('should succeed when file resources are not available in reverse mode', function (done) {
593+
temp.mkdir({}, function (err, dirPath) {
594+
var opts = {
595+
resources: [
596+
path.resolve(dirPath, 'foo'),
597+
path.resolve(dirPath, 'bar')
598+
],
599+
reverse: true
600+
};
601+
waitOn(opts)
602+
.then(function () {
603+
done();
604+
})
605+
.catch(function (err) {
606+
done(err);
607+
});
608+
});
609+
});
610+
611+
it('should succeed when file resources are not available later in reverse mode', function (done) {
612+
temp.mkdir({}, function (err, dirPath) {
613+
var opts = {
614+
resources: [
615+
path.resolve(dirPath, 'foo'),
616+
path.resolve(dirPath, 'bar')
617+
],
618+
reverse: true
619+
};
620+
fs.writeFileSync(opts.resources[0], 'data1');
621+
fs.writeFileSync(opts.resources[1], 'data2');
622+
setTimeout(function () {
623+
fs.unlinkSync(opts.resources[0]);
624+
fs.unlinkSync(opts.resources[1]);
625+
}, 300);
626+
waitOn(opts)
627+
.then(function () {
628+
done();
629+
})
630+
.catch(function (err) {
631+
done(err);
632+
});
633+
});
634+
});
635+
636+
it('should timeout when file resources are available in reverse mode', function (done) {
637+
temp.mkdir({}, function (err, dirPath) {
638+
var opts = {
639+
resources: [
640+
path.resolve(dirPath, 'foo'),
641+
path.resolve(dirPath, 'bar')
642+
],
643+
reverse: true,
644+
timeout: 1000
645+
};
646+
fs.writeFileSync(opts.resources[0], 'data1');
647+
fs.writeFileSync(opts.resources[1], 'data2');
648+
waitOn(opts)
649+
.then(function () {
650+
done(new Error('Should not be resolved'));
651+
})
652+
.catch(function (err) {
653+
expect(err).toExist();
654+
done();
655+
});
656+
});
657+
});
658+
659+
});
511660

512661
});

0 commit comments

Comments
 (0)