This is the fifth day of my participation in the August Wen Challenge.More challenges in August

Off work, continue my August challenge, how fast, tomorrow is Friday! We know that nesting callback functions can make asynchronous code synchronized, but it can cause callback hell. Let’s learn about the cliche promise.

What is a Promise?

It’s another way to write asynchronous code that solves the nesting problem of callback hell. This is a big pity, which can be concluded pending, sometimes (also called “resolved”) and rejected. This is a pity.

Xiao Ming: Trust me, my tactics have improved, this will take you to the table After a long time.Copy the code

Analysis: Xiao Ming said, this time with teammates lie down to win, is a promise, this round of the process is waiting for the result. The result is no more than two kinds, one is to continue to pit teammates, resulting in failure, the second is really to honor the promise, with teammates lie to win.

The same is true of promises. You make a promise, and it can either fail or it can succeed, and that’s something that’s going to happen in the future, which is an asynchronous function in your code, and it’s going to happen after a certain amount of time, so promises are a way of writing asynchronous code.

Promise:

1. Create a Promise object

2. Use promise objects

Const fs = require('fs') // create a promise object const p = new promise (function(resolve,reject) { Fs.readfile ('a.txt','utf8',(err,data)=>{if(err) {reject(err)}else{resolve(data)}})}) // Use the promise object.then.catch p.hen (res =>{console.log(res); }).catch(err =>{ console.log(err); })Copy the code

Resolve and reject are internal functions provided by promises, and resolve is a function that needs to be called upon success. Reject is a function that needs to be called when it fails. (key)

The implementation of. Then and. Catch in promises – changes in the promise state.

Once a promise instance object is created, the state of the promise is pending, so when everything starts, everything is new

When the promise is fulfilled and the state of the promise is changed from Pending to Resolved, when the promise is used, the successful data will be captured in the. Then method

When the promise fails to be fulfilled, the state of the promise is changed from pending to Rejected. When a promise is used, the. Catch method is automatically entered and the failed result is returned.

Then, execute the next asynchronous function, form a chain structure, and proceed with the asynchronous function synchronously, only after the last result is obtained, then proceed with the next operation, note: An asynchronous code error, do not execute the following, jump directly to the.catch throw error.

Promise packaging and chain writing

const fs = require('fs') function pro(filePath){ return new Promise(function(resolve,reject) { fs.readFile(filePath,'utf8',(err,data)=>{ if(err) { reject(err) }else{ resolve(data) } }) }) } pro('a.txt').then(res=>{ console.log(res); return pro('b.txt') }).then(res=>{ console.log(res); return pro('c.txt') }).then(res=>{ console.log(res); return pro('d.txt') }).then(res=>{ console.log(res); }).catch(err =>{ console.log(err); }) // If one error occurs, the following is not executed, and the following is directly entered into the catch // print order: A.txt -> B.txt -> c.txt ->d.txtCopy the code

The function call returns a new Promise object that determines the promise state at the next execution.

Code word is not easy, if you feel good, click a “like” and then go!