domain.bind()

domain.bind(callback)

The returned function will be a wrapper around the supplied callback function. When the returned function is called, any errors that are thrown will be routed to the domain's 'error' event.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const d = domain.create();
 
function readSomeFile(filename, cb) {
  fs.readFile(filename, 'utf8', d.bind((er, data) => {
    // if this throws, it will also be passed to the domain
    return cb(er, data ? JSON.parse(data) : null);
  }));
}
 
d.on('error', (er) => {
  // an error occurred somewhere.
  // if we throw it now, it will crash the program
  // with the normal line number and stack message.
});
doc_Nodejs
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.