The existing MD files are as follows:
part1.md

part2.md

Fish encounter. Md

Demand: Read the contents of the above three files and output.
First, review how many files are implemented by reviewing how many files are implemented by using the THEN chain call call to promise?
code show as below:
// promise THEN chain call implementation
// introduce the FS module
const fs=require('fs');
const p=new Promise((resolve,reject)=>{
fs.readFile('./part1.md',(err,data)=>{
if(err){
reject(err);
}else{
resolve(data);
}
});
});
p.then(function(value){
return new Promise((resolve,reject)=>{
fs.readFile('./part2.md',(err,data)=>{
if(err){
reject(err);
}else{
resolve([value,data]);
}
})
})
}).then(function(value){
return new Promise((resolve,reject)=>{
fs.readFile('. / Fish encounter. Md',(err,data)=>{
if(err){
reject(err);
}else{
value.push(data);
resolve(value);
}
})
})
}).then(value=>console.log(value.join('\r\n')));
First of all, we must know that the THEN method is called, the result is the promise object, and the object status is determined by the return result of the callback function. The case is used here: If the callback function returns a successful promise object, then the return value of the THEN method is a promisesTatus for the promise object for success information, follow-up chain call.
Next, look at another method --ASYNC and AWAIT combine multiple files
code show as below:
// 1, introduce the FS module
const fs=require('fs');
// read part1.md
function readPart1(){
return new Promise((resolve,reject)=>{
fs.readFile('./part1.md',(err,data)=>{
if(err){
reject(err);
}else{
resolve(data);
}
})
});
}
// Read part2.md
function readPart2(){
return new Promise((resolve,reject)=>{
fs.readFile('./part2.md',(err,data)=>{
if(err){
reject(err);
}else{
resolve(data);
}
})
});
}
// read fish encounter. Md
function readMeet(){
return new Promise((resolve,reject)=>{
fs.readFile('. / Fish encounter. Md',(err,data)=>{
if(err){
reject(err);
}else{
resolve(data);
}
})
});
}
// Await expression is placed in the Async function
// Declare an ASYNC function
async function readLyrics(){
let part1=await readPart1();
let part2=await readPart2();
let meet=await readMeet();
console.log(part1.toString());
console.log(part2.toString());
console.log(meet.toString());
}
readLyrics();
This method uses: "The value of the AWAIT P expression is the promisevalue value of the promise object P", so the async function is part1, part2, and meet is the result of reading three files, and then the output is line ~
Output results:


1.Async/Await Introduction With async/await, you can easily do what the previous promises do, it has the following characteristics: • async/await is based on Promise and is not used for normal ca...
Then the previous use of Generator+co Here we continue to talk about the way js asynchronous processing async await (ieGenerator's Syntactic Sugar) Async is short for "asynchronous", async i...
...
Talking about Async/Await Handling asynchronous with async/await Async and await async: Declare an asynchronous function (async function someName(){...}) Automatically convert regular functions intoPr...
ES8 introduces the async function, making asynchronous operations more convenient. Simply put, it is the syntactic sugar of the Generator function. Variants Asynchronous functions exist in the followi...
Await means that the function can stop running, wait for the function in await to complete, and then continue to run the function instead of waiting for await, the function continues to run....
Async and await are used together 1Parallel processing 2Error handling Reprinted from: Note: async and await are used together, and the function after await is executed to execute the next js code. ex...
Reproduced in: https: //www.cnblogs.com/newwy/archive/2010/10/30/1865314.html...
After using the fixed attribute, the fixed column and other columns are misaligned. When the fluid height and the left and right columns are fixed at the same time, this problem occurs when you slide ...
How to prohibit website access to language list in Windows 10 version 1803 or higher Since the new version 1803 of Windwos10 is further UWP, the language setting block in the previous control panel ha...