Es6 asynchronous programming Promise, es6promise

Source: Internet
Author: User

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 )//})

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.