Es6 asynchronous programming Promise, es6promise
// Introduce the module let fs = require ('fs'); // asynchronously read the file method, but synchronously Execute function read (url) {// new Promise needs to input an executor // The executor needs to input two functions resolve reject return new Promise (resolve, reject) => {fs. readFile (url, 'utf8', function (err, data) {if (err) {reject (err)} else {resolve (data );}})})}; // The disadvantage is a promise and captured twice. The two requests have no dependency, and the time overwrites // read ('. /name.txt '). then (data) =>{// let obj ={}; // obj. name = data; // read ('. /age.txt '). then (data) => {// obj. age = data; // console. log (obj); //}, (err) => {// console. log (err); //}) //}, (err) =>{// console. log (err); //}); // calls back the hellchain call // a relatively better method, and uses catch to catch exceptions in asynchronous execution. // let obj = {}; // read ('. /name.txt '). then (data) => {// obj. name = data; // return read ('. /age.txt ')//}). then (data) =>{// if promise returns promise, you can continue then // obj. age = data; // return obj // The result is passed down //}). then (data) => {// console. log (data) // separate processing result //}). catch (err) => {// console. log (err) //}); // The all method is promise, which is a built-in method of the class. If one fails to be read concurrently, the time is just a read time // The first parameter is an array, and the number of assembled promise objects // after the call, a promise instance will be returned again // The best way to write Promise. all ([read ('. /name.txt '), read ('. /age.txt ')]). then ([name, age]) =>{ // data indicates that the result type of successful promise execution is array console. log ({name, age });}). catch (err) => {console. log (err)}) // race If one fails, both fail. If one succeeds, it is rarely used // Promise. race ([read ('. /name.txt '), read ('. /age1.txt ')]). then (data) =>{/// data indicates that the result type of successful promise execution is array // console. log (data );//}). catch (err) => {// console. log (err )//})