What's that? Promises
Promises is a specification for asynchronous programming. The goal is to normalize asynchronous processing objects and processing rules. Provides a unified interface for asynchronous programming.
?
The traditional callback function
When it comes to JavaScript's asynchronous programming, we usually think of callback functions, such as the following code:
Getfileasync ("1.txt", function (error, result) { if (error) { throw error; } Processing when success is achieved);
The above code defines a function that gets the contents of the file and calls the incoming callback function back after it has been read. For the following scenarios:
? ? ? -Read the contents of file 1.txt. Its content is also a file address. We call it 2.txt.
? ? ? -Read the contents of file 2.txt. Its content is also a file address. We call it 3.txt.
? ? ? -Read the contents of file 3.txt
?
The code that uses the callback function is as follows:
?
Getfileasync ("1.txt", Function (Error1, RESULT1) { if (error1) { throw error1; } Getfileasync (RESULT1, function (Error2, result2) { if (error1) { throw error1; } Getfileasync (RESULT2, function (Error3, RESULT3) { console.log (RESULT3); });});
?
This is called the callback pyramid. Callback functions are nested very deep. The code is very bad looking. And not easy to read. And promise is the solution to flatten asynchronous callbacks. ?
?
Promises? specification
PROMISES/A (Http://wiki.commonjs.org/wiki/Promises/A) is an asynchronous Pattern programming specification developed by COMMONJS organization that provides a way to describe the concept of delay (or future) in a program. The basic idea is to run an asynchronous method. Does not clog the application. Returns a Promise object.
?
The promises/a+ (https://promisesaplus.com/) specification complements and changes the PROMISES/A specification.
?
The Promise object has three states: initial state (pending), success (fulfilled), and failure (rejected), where pending is the initial state, fulfilled and rejected are end states. The state transition relationship is: pending->fulfilled,pending->rejected.? The Promise object is converted from pending to fulfilled or After rejected . The state of this promise object will no longer occur, no matter what the change. For example, with:
?
?
Promise is an object that has the then method, then the interface is used to listen for different states of a promise.
?
Then (Fulfilledhandler, ErrorHandler, Progresshandler).
After joining Fulfilledhandler, ErrorHandler, and Progresshandler, the Promise object is formed.
Fulfilledhandler is called when the promise is loaded with data. ErrorHandler is called when promise fails, Progresshandler is invoked when the progress event is triggered.
?
var promise = Getasyncpromise ("FileA.txt");p romise.then (function (Result) { //Get the contents of the file successfully processed}, function (Error) {
Promises? Chained calls ?
The then method returns a new Promise object after the Fulfilledhandler or ErrorHandler callback is complete. Rather than the original promise object, so. The promise operation creates a chain call.
?
var promise = new Promise (function (resolve) { resolve ();}); Promise.then (function (value) { return value * *;}). Then (function (value) { return value * *;}). Then (function (value) {
?
Promises? error handling ?
The then () function receives two callback functions as a parameter.
The function that the second callback function fires when promise becomes rejected.
Promise also provides a catch () function to handle the rejected state of promise.
Look at the following code:
Promise.then (fucntion (Result) { console.log (' Got data! ', relust);}). catch (function (error) { console.log (' Error occurred! ', error);});
? The above code is actually equivalent to:
Promise.then (function (result) { console.log (' Got data ', result);},function (Error) {
Browser support:?
Promises is now part of the JavaScript standard, and almost all browsers have implemented the Promises API. Browser compatibility such as the following:
?
?
This article briefly introduces the basics of promises, hopefully we can better use promises and write code more easily.
Introduction to the JavaScript Promises specification for asynchronous programming