Câu hỏi

NodeJS often uses a callback pattern where if an error is encountered during execution, this error is passed as the first argument to the callback. What are the advantages of this pattern?

fs.readFile(filePath, function(err, data) {
  if (err) {
    // handle the error, the return is important here
    // so execution stops here
    return console.log(err)
  }
  // use the data object
  console.log(data)
})

Câu trả lời

Advantages include:

  • Not needing to process data if there is no need to even reference it
  • Having a consistent API leads to more adoption
  • Ability to easily adapt a callback pattern that will lead to more maintainable code

As you can see fr...

Bạn cần đăng nhập để xem