• callback {Function} The callback function
    • Returns: {Function} The bound function

    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.

    1. const d = domain.create();
    2. function readSomeFile(filename, cb) {
    3. fs.readFile(filename, 'utf8', d.bind((er, data) => {
    4. // If this throws, it will also be passed to the domain.
    5. return cb(er, data ? JSON.parse(data) : null);
    6. }));
    7. }
    8. d.on('error', (er) => {
    9. // An error occurred somewhere. If we throw it now, it will crash the program
    10. // with the normal line number and stack message.
    11. });