Promises is a kind of abstraction that makes code asynchronous behavior more elegant. it is likely to be the next programming paradigm of JavaScript. a Promise represents the task result, regardless of whether the task is completed or not. This document uses jQuery $. the Deferred object is used as an example to explain how the promise object handles the asynchronous problem. if you need a friend, Promises
What is promise? The core idea of promise is to represent a result of an asynchronous operation, and promise has three states (pending initial state, fulfilled success state, rejected failure state). We can understand that the use of promise can implement non-blocking IO function, according to three different states,
The resovle mechanism of angular is actually applied promise, giving us a chance to do pre-processing before entering a specific route.1. Before entering this route, lazy load the corresponding. js1 $stateProvider2. State (' Owner_detail_room ',{3URL: '/owner_detail_room/{id:[0-9]{1,10}} ',4 views:{5' Main ' : {6Templateurl:function() {return' Templates/owner_detail_room.html '},7Controller: ' Owner_detail_roomctrl '8 }9 },
Detail Api:https://github.com/kriskowal/q/wiki/api-referenceQ provides a way to implement promise, which is now much more in node. Because there is no Chinese API, I will simply translate it. Welcome to shoot Bricks ...First, Core Promise Methods1, Promise.then (onfulfilled, onrejected, OnProgress)Let's not talk about this.2, Promise.catch (onrejected)It is equivalent to: promise.then (undefined, onrejected
Promise is a pattern that operates asynchronous events in the form of a synchronous operation, avoids nesting of layers, and can chain-manipulate asynchronous events.We know that callback is the simplest mechanism when writing JavaScript async code, but this mechanism must sacrifice control flow, exception handling, and function semantics to the cost, and even let us fall into the callback pit, and promise
Promise is a way for asynchronous code to implement control flow. This way you can make your code clean, readable, and robust.For example, the callback code that you use to asynchronously process a file event:Fs.readfile ('directory/file-to-read', function (err, file) { if (Error) { //handle error else { // Do something with the file }});You may have heard that node will soon get into a callback hell, that's why. As a node dev
This article mainly introduces the asynchronous programming promise mode simple realization, and carries on the analysis to each step, the need friend can refer to the nextAsynchronous programmingJavaScript asynchronous programming, the web2.0 era of popular programming, we usually use the code, the most typical is asynchronous Ajax, send an asynchronous request, binding callback function, request response after calling the specified callback function
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 ');
}, 5000);
};
This is usually done if you want to add a callback to the function.
var showmsg = function (callback) {
settimeout (function () {
Alert (' Hello ');
Add callback Here
Callback ();
}, 5000);
};
If you are using the Easy.js
Promise Object
Promise represents the final result of an asynchronous operation, and the way in which it interacts is primarily the then method, which registers two callback functions to receive the final value of the Promise or the reason why this Promise cannot be performed.The state of
1. Create a Promise Objectvar New Promise (function(resolve, Reject) { // Asynchronous Processing // Call resolve or Reject} after processing is complete);2. To set the callback function for the Promise object after resolve or reject, you can use the Promise.then () method:Promise.then (onfulfilled, onrejected);Both onfulfilled and onrejected are functions
Promise is a good choice when dealing with asynchrony, reducing nesting levels, making code more readable, and logic clearer. ES6 adds it to the specification, JQuery 3.0 also modifies implementations to move closer to the specification (3.0 post announcements). Some of the new elements, such as. Fetch () native on the "thenable", but most of the previous APIs also rely on callbacks, at this time, we just encapsulate them, we can avoid nesting traps,
Nodejs ruin the hell is always criticized, the following summarizes some ways to solve the destruction of hell. (Temporary study of relatively shallow)1.promisePromise mode is in one of the following three states at any time: incomplete (unfulfilled), completed (resolved), and rejected (rejected). Taking the Commonjs promise/a standard as an example, the then method on the Promise object is responsible for
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
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
What is the role of promise?When there is a dependency between multiple requests (the next request will return the result of the last request), then promise's role is highlighted.Two. How do I use promise? new Promise(function(resolve,reject){ 1.处理语句 if(处理成功){ resolve([参数]); }else{
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
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.
After a few days, the promise have a little more understanding.Recently developed with Angularjs, so studied its $qfunction is not very powerful, is a simplified version of the Q.jsReference to the source code, but my grade is much worse ...As a study, I rewrote an article myself.This time is quite neat. The code is less.$q =function(ASYNCFN) {vardefer =NewDeferred (); ASYNCFN (Defer.resolve.bind (defer), Defer.reject.bind (defer)); returnd
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
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
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.