README.md (2874B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2019 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Close 22 23 > Close a file descriptor. 24 25 <section class="usage"> 26 27 ## Usage 28 29 <!-- eslint-disable stdlib/no-redeclare --> 30 31 ```javascript 32 var close = require( '@stdlib/fs/close' ); 33 ``` 34 35 #### close( fd, clbk ) 36 37 Asynchronously closes a file descriptor, so that the file descriptor no longer refers to any file and may be reused. 38 39 <!-- eslint-disable stdlib/no-redeclare --> 40 41 ```javascript 42 var openSync = require( '@stdlib/fs/open' ).sync; 43 44 var fd = openSync( __filename ); 45 close( fd, done ); 46 47 function done( error ) { 48 if ( error ) { 49 throw error; 50 } 51 } 52 ``` 53 54 #### close.sync( fd ) 55 56 Synchronously closes a file descriptor. 57 58 <!-- eslint-disable stdlib/no-redeclare --> 59 60 ```javascript 61 var openSync = require( '@stdlib/fs/open' ).sync; 62 63 var fd = openSync( __filename ); 64 65 var err = close.sync( fd ); 66 if ( err instanceof Error ) { 67 throw err; 68 } 69 ``` 70 71 </section> 72 73 <!-- /.usage --> 74 75 <section class="notes"> 76 77 ## Notes 78 79 - The difference between this API and [`fs.closSync()`][node-fs] is that [`fs.closeSync()`][node-fs] will throw if an `error` is encountered (e.g., if given an invalid file descriptor) and this API will return an `error`. 80 81 </section> 82 83 <!-- /.notes --> 84 85 <section class="examples"> 86 87 ## Examples 88 89 <!-- eslint-disable stdlib/no-redeclare --> 90 91 <!-- eslint no-undef: "error" --> 92 93 ```javascript 94 var join = require( 'path' ).join; 95 var openSync = require( '@stdlib/fs/open' ).sync; 96 var close = require( '@stdlib/fs/close' ); 97 98 var err; 99 var fd; 100 101 /* Sync */ 102 103 fd = openSync( join( __dirname, 'package.json' ), 'r+' ); 104 if ( fd instanceof Error ) { 105 console.error( fd.message ); 106 } else { 107 err = close.sync( fd ); 108 // returns undefined 109 110 if ( err instanceof Error ) { 111 console.error( err.message ); 112 } else { 113 console.log( 'Synchronously closed file descriptor.' ); 114 } 115 } 116 117 /* Async */ 118 119 fd = openSync( join( __dirname, 'package.json' ), 'r+' ); 120 if ( fd instanceof Error ) { 121 console.error( fd.message ); 122 } else { 123 close( fd, done ); 124 } 125 126 function done( error ) { 127 if ( error ) { 128 console.error( error.message ); 129 } else { 130 console.log( 'Asynchronously closed file descriptor.' ); 131 } 132 } 133 ``` 134 135 </section> 136 137 <!-- /.examples --> 138 139 <section class="links"> 140 141 [node-fs]: https://nodejs.org/api/fs.html 142 143 </section> 144 145 <!-- /.links -->