all = $q.all([one.promise, two.promise, three.promise]);all.then(success);
What is promise?
Promise is a method to process values asynchronously. promise is an object that represents the final possible return value or thrown exception of a function, when dealing with a remote object, we can regard it as a proxy of the remote object. If
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
object, new
Ajax request data code, in the callback method of the returned data
If the resolve () method of the Deferred object is successfully called, parameter: returned data
If the call to the reject () method of the Deferred object fails, the parameter: returned data
Returns the Deferred. promise object.
Call the ajax () method to obtain the promise object, parameter: url,
Call the then () method of th
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
Do not want to write too many words today, just two pieces of code, my most useful harvest today should be in this code, I will share them out of the code fragment a
var p = promise.resolve ();
var ret = null;
Console.log ("Outer1");
var ary = [to];
for (var I of Ary) {
console.log ("Test-for", I);
p = p.then (function (val) {
console.log ("Test-promise", I);
Console.log (val);
return Val;}
);
Console.log ("Outer2");
You can simpl
Promises and Design Patterns Well written long good long long ~
The traditional way to solve a Javascript asynchronous event is to call a callback function, invoke a method, then give it a function reference, and execute the function reference when the method ends. $.get (' Api/gizmo/42 ', function (Gizmo) { console.log (gizmo);//or whatever}); It looks great, right, but there are drawbacks; first, merging or linking multiple asynchronous processes is super-complex; either a lot of temp
Javascript uses a callback function (callback) to handle asynchronous programming. There is an adaptive process from synchronous programming to asynchronous callback programming, but if there is a multi-layer callback nesting, which is what we often call the doom-correction pyramid (Pyramid of Doom), it's definitely a bad programming experience. Then there is the Commonjs promises/a specification, which is used to solve the problem of the callback pyramid. This article first introduces the promi
Asynchronous mode is becoming more and more important in web programming, which is not very easy to implement for Web-leading language JavaScript, so many JavaScript libraries (such as jquery and Dojo, AngularJS) Adds an abstraction called promise (the term is called deferred mode). Through these libraries, developers can use the promise pattern in real programming, each
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
(function(results) { returnresults[0].save({ key: value });}).then(function(result) { // the object was saved.},function(error) { // there was some error.});
Generally, developers think that an asynchronous promise failure is equivalent to throwing an exception. In fact, if a callback throws an error, promise returns the failure message. Passing an error to the next available error processor is equivalent t
exception, and when dealing with a remote object we can think of him as a proxy for a remote object. If promise is also a kind of asynchronous processing, then we will think of it and XHR and $.ajax what is the difference?
It is customary for JS to use closures or callbacks to correspond to data returned asynchronously, such as a XHR request after a page is loaded. We can interact with the data as if it h
returned data
If you fail to invoke the Reject () method of the deferred object, parameter: The returned data
Returns the Deferred.promise object
Invoke the Ajax () method to get the Promise object, parameter: URL,
Call the Promise object's then () method, parameter: anonymous function
Call the Ajax () method, get to the Promise object, return this object
(this.resolve)3. Execute Promisecallback Our logic jumps to the inside of the Promisecallback function.setTimeout(() => { resolve("延时执行")}, 1000)Logic is simple, is to wait 1 seconds after the execution of the Resolve function, which resolve?Promisecallback (Resolve), FN (this.resolve), resolve4. Execute resolve our logic jumps to the inside of the resolve function.that.cb(data)Where does this that.cb come from? is the callback function in the then parenthesis that we saved in the first step, w
IntroductionIn the classic article about Deferred usage in jQuery1.5 (for the translation, see here), we will provide the following description:$. Ajax () returns an object packed with other deferred-related methods. I discussed promise (), but you'll also find then (), success (), error (), and a host of others. you don't have access to the complete deferred object, though; only the promise, callback-bindi
promise object in JavaScript
Many people who do the front end should have heard of promise (or deferred) objects, today I will tell about my understanding of promise
What?
Promise is one of the COMMONJS specifications, with resolve, reject, done, fail, then and other methods, can help us control the flow of code, t
libraries, and should.js is one of them. In the code above, a BDD-style assertion is implemented using Should.js.Next, we perform the tests:$ Mocha Test/chapter1/synchronous.jsWe will see the following output: Calculator #add ? 1 + 2 = 3 ? 2-1 = 1 2 passing (9MS)Asynchronous code (callback) testWhen you say JavaScript, you must have a callback function. From the front-end Ajax communication to node. JS asynchronous data access, the cal
1 Promise Overview
The Promise object is a specification proposed by the COMMONJS workgroup to provide a unified interface for asynchronous operations.
So, what is promises.
First, it is an object, that is, the use of other JavaScript objects, and second, it acts as an agent (proxy), acting as an intermediary between an asynchronous operation and a callback function. It enables the asynchronous operation t
libraries, and should.js is one of them. In the code above, a BDD-style assertion is implemented using Should.js.Next, we perform the tests:$ Mocha Test/chapter1/synchronous.jsWe will see the following output: Calculator #add ? 1 + 2 = 3 ? 2-1 = 1 2 passing (9MS)Asynchronous code (callback) testWhen you say JavaScript, you must have a callback function. From the front-end Ajax communication to node. JS asynchronous data access, the cal
Preface
What is to be said here is a redux in the react of the application of the problem, talk about Redux,react-redux,redux-thunk,redux-actions,redux-promise, Redux-sage the effects of these packages and the problems they solve.Because do not want to take the space too long, so there is not too much source code analysis and grammar explanation, can be how simple simple.Redux
First look at the Baidu encyclopedia above redux a picture:
This is Redux'
1190000000684654What?PromiseIs CommonJS one of the specifications, the own,,,, and resolve reject done fail then so on, can help us control the flow of code, avoid multi-layered functions of nesting. Today, Async is becoming more and more important in web development, and for developers, this non-linear execution of programming can be difficult for developers to control, and allows Promise us to better control the code execution process, and jQuery so
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.