Promise ES6 asynchronous operation of

Asynchronous operation that has been in my mind like a cloud of fog, but it is important to us non-science is not, then it's veil to unlock it.

ES6 before birth, asynchronous programming methods, about the following four.

  • Callback
  • Event Listeners
  • Publish / Subscribe
  • Promise objects

Asynchronous : Simple: a continuous execution order is not complete, add other intermediate calculation program, and other data ready for the first stage, and then returned to be calculated.

Take a look at entry-speaking teachers in Ruan Yifeng's es6

Repeatedly read several times, really fully understand what is asynchronous.

Promise is asynchronous programming solution, called Promise, it simply is a container, which holds the results of the future will be the end of the event (that is, results of asynchronous operations), Promise is an object, you can get information from the asynchronous operation of it.

Only, Promise's role is to act as "the second stage (with files) then went on mission", processing files can be seen a large box and read the file based on the example of the above process the file, Promise thing to do operational information, save the results of execution.

Feature

  1. There are three states panding (in progress), fulfilled (been successful) and rejected(failed), the state free from outside influence
  2. Once the change in status does not change (resolved (setting))

           panding >>>fulfilled

   panding>>>rejected

advantage

  With Promisethe object can be asynchronous operation to synchronous operation flow express avoided deeply nested callback

Shortcoming

  1. Can not be canceled during
  2. Do not set a callback function, internal promise throw an error will not affect the external
  3. When in a pending no way of knowing where progress

Basic Usage

  Share section of the code of the actual development, in conjunction with theoretical knowledge, it is estimated that you will know the basic usage, and how to use the built-in method.

  

 

Guess you like

Origin www.cnblogs.com/cqy1125/p/11384450.html