UNPKG

638 BJavaScriptView Raw
1
2exports.async = function (fn, cb) {
3
4 var tries = 3;
5
6 (function next () {
7 fn(function (err, res) {
8 if (!err) { return cb(null, res); }
9 if (err.code !== 'EEXIST') { return cb(err); }
10
11 if (--tries === 0) {
12 cb(new Error('Failed to find unique name'));
13 } else {
14 next();
15 }
16 });
17 }());
18
19};
20
21exports.sync = function (fn) {
22
23 var tries = 3;
24
25 while (--tries) {
26
27 try {
28 res = fn();
29 } catch (err) {
30 if (err.code === 'EEXIST') {
31 continue ;
32 } else {
33 throw err;
34 }
35 }
36
37 return res;
38 }
39
40 throw new Error('Failed to find unique name');
41};