is ENOENT from fs.createReadStream uncatchable?

前端 未结 1 1324
独厮守ぢ
独厮守ぢ 2021-02-02 06:58

I\'m not able to catch ENOENT of fs.createReadStream(). Is this an asynchronous function , which throws exception in a different closure-chain ?

$ node -v
v0.10         


        
相关标签:
1条回答
  • 2021-02-02 07:32

    fs.createReadStream is asynchronous with the event emitter style and does not throw exceptions (which only make sense for synchronous code). Instead it will emit an error event.

    const fs = require('fs')
    
    const stream = fs.createReadStream('foo');
    stream.on('error', function (error) {console.log("Caught", error);});
    stream.on('ready', function () {stream.read();});
    
    0 讨论(0)
提交回复
热议问题