0
By instantiating the following Object
class Output {
constructor(filePath) {
this.stream = undefined
this.nameFile = filePath
this.fileOk = false
this.file = this.createFile(this.nameFile)
this.file.then().catch()
}
// Cria um arquivo para salvar o relatório de debug
createFile(filePath) {
let stream = ()=>{// Cria uma stream
this.stream = fs.createWriteStream(filePath, {flags: 'a' })
}
return new Promise( (res, rej)=>{
let nameFile = this.nameFile
fs.open(filePath, "w", function(err) {
if (err) { // Caso de erro
rej()
} else {
res(stream())
}
})
})
}
Give the following errors
(Ode:19531) Unhandledpromiserejectionwarning: Undefined
(Ode:19531) Unhandledpromiserejectionwarning: Unhandled Promise rejection. This error either originated by Throwing Inside of an async Function without a catch block, or by rejecting a Promise which was not handled with . (catch). To terminate the Node process on unhandled Promise rejection, use the CLI flag
--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (Ode:19531) [DEP0018] Deprecationwarning: Unhandled Promise rejections are deprecated. In the Future, Promise rejections that are not handled will terminate the Node.js process with a non-zero Exit code.
how should I treat error?
The best way to handle Promise errors is by using Try catch. , but there are other ways I wouldn’t recommend because you might face bigger problems later. is known as callback error is no longer so used.
– Luciano Alves