, the then method can accept two parameters, the first corresponding to the resolve callback, and the second to the reject callback. So we were able to get the data that they had handed over. Run this code multiple times, and you'll randomly get the following two kinds of results:
Or
Use of CatchWe know that the Promise object has a catch method in addition to the then method, what does it do? In fact, it is the same as the second parameter of then, w
Promise object in Angular ($ q Introduction)
This article mainly introduces the Promise object in Angular ($ q Introduction). This article describes the Promise mode, the basic usage of Q Promise, and the $ q in AngularJs. defferd and other content. For more information, see
When using JQuery, we know that
: ' // add error events at the same time var p = new Promise (function (resolve, reject) {resolve ( "OK" ); throw new Error (' Error0 ' ); SetTimeout ( function () {throw Span style= "color: #0000ff;" >new Error (' Error1 ')}, 0 function (value) {Console.log ( Value)}). catch (Funcrion (err) {Console.log (Err)}) ;Process.on (function (err, p) {console.error (' catch exception: ', Err.stack)});First put the problem here, if one ca
This article mainly introduces the Promise object in Angular ($ q introduction). This article describes the Promise mode, the basic usage of QPromise, and $ q in AngularJs. defferd and other content. if you need it, you can see that when using JQuery, you will know that promise is a Js asynchronous programming mode, bu
This article mainly introduces the Promise object in Angular ($ q Introduction). This article describes the Promise mode, the basic usage of QPromise, and $ q in AngularJs. defferd and other content. If you need it, you can see that when using JQuery, you will know that promise is a Js asynchronous programming mode, bu
: {then:function(){ if(Pending) {Pending.push (callback); }Else{callback (value); } } } }; returnself.promise;};Implement Promise CascadeThe next step will be a very important one. To the front of the Q3, the implementation of the promise are not cascading. But the promise you are familiar with sh
(1) Why use this design pattern of promise, what are the benefits of this design pattern?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.Artifact from Javascript--promisepromise in JS
callback function The real problem is that he deprives us of the
Promise in JS
callback function The real problem is that he deprives us of the ability to use return and throw these keywords. And Promise a good solution to all this.
In June 2015, the official version of ECMAScript 6 was finally released.ECMAScript is an international standard for JavaScript, and JavaScript is the implementation of ECMAScript. The
The promise in the angular
When you use jquery, you know promise is a pattern of JS asynchronous Programming mode, but it's not quite clear what difference he has with jquery's deferred object. With the company project, to the background with the data, so decided to fix it.
Promise
What is promise
Promise is a more elegant pattern of writing asynchronous code, allowing asynchronous operation code to be written and read like synchronous code, such as the following example of an asynchronous request:
$.get ("/js/script,js", function () {
//callback to do
})
Can be rewritten as
When you use jquery, you know promise is a pattern of JS asynchronous Programming mode, but it's not quite clear what difference he has with jquery's deferred object. With the company project, to the background with the data, so decided to fix it.
Promise
Promise is a mode in which asynchronous events are manipulated
What's Angularjs?
Angularjs was born in 2009, by Misko Hevery and other people to create, after the acquisition of Google. is an excellent front-end JS framework, has been used in a variety of Google products. Angularjs has many characteristics, the most core is: MVVM, modular, automated two-way data binding, semantic tagging, dependency injection, and so on
Promise
specify the resolve and reject callbacks respectively.
promise.then(function () { console.log(' resolve from promise');}, function () { console.log(' reject from promise');});
Result 1:
begin do something run success resolve from promise
Result 2:
begin do something run failed reject from promise
Use
Issues caused by low-version browsersRecently developed a project based on webpack+babel+react , generally local is in chrome browsing above development, Chrome browser development because support most of the new JS features, so generally do not need polyfill, such as Promise,string instance includes method. Even in the low version of the browser, through babel-runtime the Polyfill can also be converted, bu
() supports two parameters. The first parameter is enabled when Promise is successful, and the second parameter is started when Promise fails. Use. catch () to achieve the same effect. In addition to better readability, the returned Promise object can also be passed at will to continue the chain call, which is quite imaginative.
Continue. then ()So we might as w
Watchutils.when both methods return is the Watchhandle object. After the study after more monitoring, and then carefully look at other monitoring methods.The difficulty lies in the then method.Difficulties.Then () How did the method come? This starts with the new specification Promise object for ES6 (full name ECMAScript 2015). ECMAScript is the standard of JavaScript, JS is the implementation of ES.What i
function are resolve and reject respectively. They are two functions that are provided by the JavaScript engine and do not have to be deployed on their own.
The role of the resolve function is to change the state of the promise object from incomplete to successful (that is, from pending to resolved), to be invoked when the asynchronous operation succeeds, and to pass the result of the asynchronous operation as a parameter;The role of the Reject funct
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.
Var showMsg = function (){
SetTimeout (function (){
Alert ('hello ');
},5000 );
};
This is usually the case if you want to add a callback to the function.
Var showMsg = function (callback ){
SetTimeout (function (){
Alert ('hello ');
// Add a callback here
Callback ();
},5000 );
};
If you use Promise of
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
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
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.