-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (22 loc) · 826 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const lf = require('proper-lockfile');
const fs = require('fs')
const path = require('path')
function Lock( directory, fname ) {
this.lockDirectory = directory;
this.lockfilePath = path.join( directory, fname );
return this;
}
Lock.prototype.runlocked = function( func ) {
let userResult, userError, release;
return lf.lock( this.lockDirectory, { lockfilePath: this.lockFilePath } )
.then( result => release = result )
.then( function() {
return Promise.resolve()
.then( () => func() )
.then( result => userResult = result )
.catch( err => userError = err )
})
.then( () => release() )
.then( () => userError ? Promise.reject( userError ) : userResult )
}
module.exports.Lock = Lock