Use Promise to fix callback hell?

First, if you use normal function nested calls, you can create callback hell
// Use Promise to fix callback hell

// Import fs module
const fs = require('fs')

function getReadFile(fpath) {
  var promise = new Promise(function (resolve, reject) {
    fs.readFile(fpath, 'utf-8'.(err, dataStr) = > {
      if (err) return reject(err)
      resolve(dataStr)
    })
  })
  / / return promise
  return promise
}

// Read file 1, read file 2, read file 3
// This is not the case
getReadFile('./files/1.txt').then(
  (data) = > {
    console.log(data + 'File 1 read successfully')
    getReadFile('./files/2.txt').then(
      (data) = > {
        console.log(data + 'File 2 read successfully')
        getReadFile('./files/3.txt').then(
          (data) = > {
            console.log(data + 'Read file 3 succeeded')},(err) = > {
            console.log(err + 'Failed to read file 3')})},(err) = > {
        console.log(err + 'Failed to read file 2')})},(err) = > {
    console.log(err.message + 'File reading failed')})Copy the code
Second, solutions
// Use Promise to fix callback hell

// Import fs module
const fs = require('fs')

function getReadFile(fpath) {
  var promise = new Promise(function (resolve, reject) {
    fs.readFile(fpath, 'utf-8'.(err, dataStr) = > {
      if (err) return reject(err)
      resolve(dataStr)
    })
  })
  / / return promise
  return promise
}

// Read file 1, read file 2, read file 3
// Correct operation
getReadFile('./files/1.txt')
  .then(
    (data) = > {
      console.log(data + 'File 1 read successfully')
      // Return the Promise instance, avoiding the problem of callback hell
      return getReadFile('./files/2.txt')},(err) = > {
      console.log(err.message + 'Failed to read file 1')
    }
  )
  .then(
    (data) = > {
      console.log(data + 'File 2 read successfully')
      // Return the Promise instance, avoiding the problem of callback hell
      return getReadFile('./files/3.txt')},(err) = > {
      console.log(err.message + 'Failed to read file 2')
    }
  )
  .then(
    (data) = > {
      console.log(data + 'Read file 3 succeeded')},(err) = > {
      console.log(err.message + 'Failed to read file 3')})Copy the code

Running results:

Three, Promise two ways to catch exceptions?
getReadFile('./files/1.txt')
  .then(
    (data) = > {
      console.log(data + 'File 1 read successfully')
      return getReadFile('./files/2.txt')},(err) = > {
      console.log(err.message + 'Failed to read file 1')
      return getReadFile('./files/2.txt')
    }
  )
  .then(
    (data) = > {
      console.log(data + 'File 2 read successfully')
      return getReadFile('./files/3.txt')},(err) = > {
      console.log(err.message + 'Failed to read file 2')
      return getReadFile('./files/3.txt')
    }
  )
  .then(
    (data) = > {
      console.log(data + 'Read file 3 succeeded')},(err) = > {
      console.log(err.message + 'Failed to read file 3')})Copy the code

Execution Result:

3.2. Requirements: Subsequent promise execution depends on the results of previous promise execution. If the previous promise execution fails, there is no need for subsequent promise execution

getReadFile('./files/1.txt')
  .then((data) = > {
    console.log(data + 'File 1 read successfully')
    return getReadFile('./files/2.txt')
  })
  .then((data) = > {
    console.log(data + 'File 2 read successfully')
    return getReadFile('./files/3.txt')
  })
  .then((data) = > {
    console.log(data + 'Read file 3 succeeded')
  })
  .catch((err) = > {
    // Catch: If any previous Promise fails, all Promise execution is immediately terminated and the catch is immediately entered to handle the exception raised in the Promise.
    console.log(err.message)
  })
Copy the code

Execution Result: