Promise:Before ES6, if we need to do asynchronous processing in JS, most are solved by using callback functions, and if simple asynchronous processing, the callback function looks elegant, the force is still a bit high, but if there are many asynchronous operations, the callback nesting is very deep, the code will look particularly awkward , maintenance costs will also become higher this time ES6 bring promise this new feature, this method is a good s
Promise DefinitionPromise is one of COMMONJS's specifications, including methods such as Resolve,reject,done,fail,then, which can help us control the flow of code and avoid multi-layered nesting of functions. Async is becoming more and more important in web development, and for developers, the programming of J-non-linear execution can be difficult for developers to control, and promise allows us to better
Native promise objects have been provided in some modern browsers that follow the promise/a+ standard. In jquery1.5+, a $ is provided. Deferred (which can be converted to promise objects). Many well-known frameworks also provide promise objects. The Promise object is already
is nested. If several layers need to be nested, then you have to be blessed.Let's take a look at this common asynchronous function.
Copy codeThe Code is as follows:Var showMsg = function (){SetTimeout (function (){Alert ('hello ');},5000 );};
This is usually the case if you want to add a callback to the function.Copy codeThe Code is as follows:Var showMsg = function (callback ){SetTimeout (function (){Alert ('hello ');// Add a callback hereCallback ();},5000 );};
If you use
Before the text: About style is the CSS of the page is not a comment, because the official example of the style is too simple, copy read.This article has a lot of AJS 4.x version added content, such as listening watch, promise object, callback function, asynchronous processing and other content, the original rational things I will explain at the end of the text, you crossing don't worry about not understand, I try to explain these in popular language.
, the code organization also can be very not graceful, greatly reduces the code maintainability. The situation is more complex, if an operation to wait until the completion of more than one asynchronous Ajax request, there will be a callback function nesting situation, if you need to nest several layers, then you can only ask for more blessings.
Let's take a look at the common asynchronous function below.
var showmsg = function () {
settimeout (function () {
Alert (' Hello ');
}
The new standard adds to native promise. This is just a case of chained use, think about the details. One, review of then () and catch ()Then () and catch () parameters can be placed in the callback function to receive the final result of a promise.Then () can receive a parameter, then this callback will only be called when Promise Resolve ().Then () can also receive the second parameter, then the second ca
Promise
Promise has become one of the most important tools for JavaScript to manage asynchronous operations. However, sometimes it can be a headache:
Promise
//equivalent to ' promise.resolve (undefined). Then '.
Then (() => {
//start.
})
. Then (() => {
if (wanttobreakhere) {
//How to terminate this
Summary of how to use javascript Promise, javascriptpromise
The nested callback function is too deep, and the parallel logic must be executed in a serial manner. A Promise represents the final result of an asynchronous operation. The main way to interact with Promise is through its then () method to register the callback function and receive the final result valu
First, describeWe know that the JavaScript implementation environment is "single-threaded", so-called single-threaded, is only able to perform one task at a time, if there are multiple tasks to queue, the previous task is completed before you can continue the next task.This "single-threaded" advantage is simple to implement, easy to operate, the disadvantage is that it is prone to blocking, because if a task in the queue time is longer, then the subsequent tasks can not be executed quickly, or c
Asynchronous mode becomes more and more important in Web programming. It is a headache to handle the operations after asynchronous requests. Promise is an asynchronous programming model called the Deferred mode. It standardizes asynchronous operations through a set of APIS, Making Process Control of asynchronous operations easier. The Promise programming mode is also called thenable, which can be understood
For example, explain how to use Promise in JavaScript, javascriptpromise
Excerpt-the Parse JavaScript SDK now provides a Promises mode compatible with jquery that supports most asynchronous methods. What does this mean? You will understand it after reading the following.
"Promises" represents the next great paradigm in javascript programs, but it is not easy to understand why they are so great. At its core, a prom
First talk about what is promise, what is $q bar. Promise is an asynchronous processing model, with many implementations, such as the famous Kris Kwal ' s Q and the deffered of jquery.
What is promise
Previous knowledge of Ajax has been able to experience the pain of callback, synchronized code is easy to debug, but the asynchronous callback code, will let the
When dealing with promise in angularjs, it sometimes comes across situations where you need to handle multiple promise.
The simplest treatment is that every promise is then. As follows:
var app = Angular.module ("App", []);
App.controller ("Appctrl", Function ($q. $timeout) {
var one = $q. Defer ();
var two = $q. Defer ();
var three = $q. Defer ();
$time
The so-called promise is simply a container that holds the result of an event (usually an asynchronous operation) that will end in a future.
Promise is an object from which you can get messages for asynchronous operations.
Function Promise () {
new Promise (() => {
settimeout (() => {
console.
basic usage of promise
var p1 = new Promise (resolve, Reject) => {
settimeout (Resolve, 1000, ' done ');
}
Promise Use function Pajax in P1.then (data=>{console.log (data),//Done
})
//angular
(dataurl,data {
var deferred = $q. Defer ();//Declaration deferred execution, indicating that you want to monitor subsequent execution
$http ({
Ur
1 //function function: Create a random integer within 10 after 1 seconds, and determine if the number is even, and if it is even, do one thing, and if it is odd, do another thing .2 functiondosomthing () {3 varPromise =NewPromise (function(Resolve, reject) {4SetTimeout (function () {5Const num = Math.Round (math.random () * 10);6 if(num% 2 = = = 0) {7 resolve (num);8}Else {9 reject (num);Ten } One}, 1000); A }); -
Convert the applet asynchronous API to promise. How easy it is to handle asynchronous operations with promise, who knows who.The official does not give the Promise API to handle asynchronous operations, and the official API is very asynchronous, which makes the multi-asynchronous programming layer callback, the code is complex, callback to hit the computer.A gene
Read Catalogue
What is promise
$Q Services
First say what is promise, what is $q bar. Promise is an asynchronous processing mode that has many implementations, such as the famous Kris Kwal's Q and the deffered of jquery.
Back to the top what is promiseI've learned about Ajax before, and it's easy to debug a synchronous code, but t
Today a friend sent over a section of JS code, did not understand, the way to learn a bit, the code is as followsPromise.resolve (' Zhangkai '). Then (value = = {Console.log (value)}) was searched to know that it was a Promise application, so take notes, organize them, and use them in your project.What is a Promise objectThe Promise object is the unified interfac
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.