Promise

  • The callback hell

In other words, asynchrony is nested within asynchrony, and asynchrony depends on the results of the previous asynchrony

const fs = require("fs");
fs.readFile('./data/a.txt'.function (err, data) {// String binary to Chinese can be set to 'utf8' in the second argument here
    if (err) {
        // return console(' read failed ')
        // Throw an exception
 // todo 1. Block program execution  Todo 2. Print the error message to the console  throw err  }  console.log('xxx', data.toString()) }) fs.readFile('./data/b.txt'.function (err, data) {// String binary to Chinese can be set to 'utf8' in the second argument here  if (err) {  throw err  }  console.log('xxx', data.toString()) }) fs.readFile('./data/c.txt'.function (err, data) {// String binary to Chinese can be set to 'utf8' in the second argument here  if (err) {  throw err  }  console.log('xxx', data.toString()) }) // The order of output depends on the size of the file and it doesn't have to be a multithreaded thing at the bottom Copy the code
  • Order is guaranteed by nesting callbacks
const fs = require("fs");
fs.readFile('./data/a.txt'.function (err, data) {// String binary to Chinese can be set to 'utf8' in the second argument here
    if (err) {
        // return console(' read failed ')
        // Throw an exception
 // todo 1. Block program execution  Todo 2. Print the error message to the console  throw err  }  console.log('xxx', data.toString())  fs.readFile('./data/b.txt'.function (err, data) {// String binary to Chinese can be set to 'utf8' in the second argument here  if (err) {  // return console(' read failed ')  // Throw an exception  // todo 1. Block program execution  Todo 2. Print the error message to the console  throw err  }  console.log('xxx', data.toString())  fs.readFile('./data/c.txt'.function (err, data) {// String binary to Chinese can be set to 'utf8' in the second argument here  if (err) {  // return console(' read failed ')  // Throw an exception  // todo 1. Block program execution  Todo 2. Print the error message to the console  throw err  }  console.log('xxx', data.toString()) }) }) }) //b must be placed in the callback of A Copy the code
  • In order to solve the problem (callback hell nesting), an API has been added to ES6:Promise

  • It starts off as pending and the state can only be one or the other
  • There’s a new API in ES6, and promise is a constructor
// Todo adds an API in ES6, and Promise is a constructor
const fs = require('fs')
// 1. Create a Promise container to give someone a Promise
console.log(1)
The promise itself is not asynchronous. The function (task) inside the promise is asynchronous
var p1 = new Promise(function(resolve,reject){/ / parameter  //p1 is an instance of new Promise  console.log(2)  fs.readFile('./data/a.txt'.'utf8',(err,data)=>{// Promise is a container but usually encapsulates an asynchronous task  if(err){  // Failed the task in the promise container failed  // console.log(err)  reject(err)Reject (reject); reject (reject)  }else{  // The task in the promise container succeeded  console.log(3)  // The resolve method is actually the function passed in the then method  resolve(data)The Resolved callback is called here to change the container's pending state to resolved  }  }) })  console.log(4) // P1 is a promise // When p1 succeeds, do the specified operation The accepted function in then is the resolve function in the container p1.then(function(data){  console.log(data)// Define this data as the data in resolve },function(err){  console.log('Output file failed',err)/ / reject method }) Copy the code
  • Create three promise instances
p1,p2,p3
p1.then(function(data){
    console.log(data)
    Return (p1, p1, p1, p1, p1, p1, p1, p1, p1)
    // if 123 is not returned, undefined is returned
 // When return a promise object, the first argument to the then method will be used as P2's resolve  return p2 },function(err){  console.log('Output file failed',err)/ / reject method }).then(function(data){/ / p2 resolve  console.log(data)  return p3 }).then((data) = >{/ / the resolve of p3  console.log(data) })// chain call Copy the code
  • Encapsulate the Promise version of readFile
// Todo adds an API in ES6, and Promise is a constructor
const fs = require('fs')

function pReadFile(filePath){
    return new Promise(function(resolve,reject){/ / parameter
 // console.log(2)  fs.readFile(filePath,'utf8',(err,data)=>{  if(err){  // Failed the task in the promise container failed  // console.log(err)  reject(err)Change the pending state of the container to reject  }else{  // The task in the promise container succeeded  // console.log(3)  // The resolve method is actually the function passed in the then method  resolve(data)// Change the pending state of the container to Resolved  }  })  }) } pReadFile('./data/a.txt')  .then(function(data){// Promise objects can be then  console.log(data)  return pReadFile('./data/b.txt')// This method returns a promise object when called  })  .then(function(data){  console.log(data)  return pReadFile('./data/c.txt') }) .then(function(data){  console.log(data) }) Copy the code

See article: From asynchronous concepts to callback callback functions to Promises

es6promise