NodeJS-based Promise-based shard skin read/write MongoDB
Promise is an asynchronous programming code writing method. Like the Async module, Promise can improve code readability and maintainability. Async encapsulation is better than Promise, but the flexibility, especially the thorough CallBack conversion, is worse tha
Excerpt –parse The JavaScript SDK now offers a promises model that supports most asynchronous methods, so what does that mean, and you'll understand when you read the following.
"Promises" represents the next great paradigm in JavaScript programs, but it is not easy to understand why they are so great. Its core is that a promise represents a task that may or may not have been completed. The only interface required for
Promise is one of the COMMONJS specifications, with resolve, reject, done, fail, then and other methods, can help us control the flow of code, to avoid the multi-level nesting of functions. Now asynchronous is becoming more and more important in web development, for developers, this non-linear execution of the programming will make the developers feel difficult to control, and promise can let us better cont
What's the difference between deferred and promise?
Promise
A promise is an object that is returned by an asynchronous function. When you want to write one of these functions yourself, you need to use a deferred.
var promise = $.ajax ({
URL: "/myserverscript"
});
Promise.done (mysuccessfunction);
Promise.fai
In fact, about promise blog, the front-end time dedicated to write a promise specification of the article, Promise specification makes JavaScript in the asynchronous call more humane.Simple recall under:The "deferred/promise" model defined by the PROMISE/A specificationIn fa
The promise internally creates a shared state that is used to put a corresponding type of value or an exception, and can be taken by the future object as the thread resultsPromise the results into a shared state after the result is formed. So there is no concurrent operation to read and write#include #include#include#includestring>#include#include//random Number class#include //Time Processing Class#include //Standard Exception class#include //STL-def
that when the deferred state is switched (called reject or resolve later), and then the callback function is bound, then the corresponding state of the callback function will be executed immediately.As an example:deferredYou can also return your own subset of operations.promiseThere is only one API that binds the callback, and no stateful switching API.Deferred: reject, resolve, done, fail, then, alwaysPromise: Done , fail, then, alwaysis obviously promise
I learned about async and promise a long time ago, but always smattering.Today, when writing Nodejs, found a lot of third-party libraries using callbacks, so in the actual operation will appear multiple callbacks, this is the legendary JS callback hell.As an example,There is a way to call Redis, access a hash object table, get the return value, and insert the return value into the other hash object table of Redis.Testcallbackaction () {Let Redis = red
Here is a summary of the usage and description of Q in Node. js, without introducing promise and Principles.* Promise is to solve the JS in the callback hierarchy too deep code difficult to understand the problem of Change.Q is one of the promise packages implemented in nodejs, which is a common library in Nodejs.How to install Q in your project:NPM Install Q-sav
A normal function is encapsulated into a promise instance, there are 3 key steps, the first step is to construct a promise instance inside the function, the second step is to complete the execution of the deployment function to change the state of promise, and the third step is to return the promise instance. Each
JQuery's show and hide methods are well known and know that it can accept a callback function for execution after the animation completes. Like what:
$ ('. Js-sam '). Hide (' slow ', function () {Alert (' You just hid Sam ');});
However, the JQuery 1.8 version also provides another writing style, namely promise– this Chinese translation is very difficult, now see there is a promise-I still continue to use
Recently has been studying JS promise object, which has a blog written relatively easy to understand, forwarded as follows:Http://www.cnblogs.com/lvdabao/p/es6-promise-1.htmlReferring to the described above, I wrote the test example, understand the use of promise, and then wrote a FS promise example, the code is as fol
encapsulating Ajax and Promise
First, Ajax
Ajax is called "Asynchronous JavaScript and XML" (Asynchronous JavaScript and XML) refers to the development of interactive Web applications to create a technology, improve the user experience, achieve no refresh effect.
Advantages:A, no plug-in support requiredb, the page does not refresh, the use of asynchronous way to communicate with the server, with more rapid response capabilities, excellent user expe
20060325
This topic is a bit out of fashion, but I just remembered that I did not see the promise before I saw the blood of Shantou, and I was not interested in watching the promise. I would not have liked Chen Kaige's film. When the promise was raging across China, I was also indifferent. I only knew that there was a
What is promiseThere are many asynchronous implementations in JavaScript, delay functions, callback functions, and ES6 new generator functions, where promise is also one of the ways to implement Asynchrony.About the asynchronous implementation of JavaScript in detail and the difference, the post-update blog, please look forward to, no longer repeat here.$Q ServicesIn short $q service is a promise implementa
is worth noting that when the deferred state is switched (called reject or resolve later), and then the callback function is bound, then the corresponding state of the callback function will be executed immediately.As an example:Deferred can also return its own subset of operations.promiseThere is only one API that binds the callback, and no stateful switching API.deferred: reject, resolve, done, fail, then, alwaysPromise: Done , fail, then, alwaysis obviously
Copyright NOTICE: This work is licensed under the Creative Commons Attribution-NonCommercial use-Share 4.0 International license agreement in the same way. Reprint please indicate source Http://blog.csdn.net/azureterniteDirectory (?) [+]The concept of promise
The Promise object is used for asynchronous (asynchronous) computations: A Promise object repres
Promise is to solve the callback! There's a guy called Q in the NPM package that specializes in encapsulating promise to solve callback problems. About promise Introduction and Q's introductory point here. Here's how to use Q to encapsulate an API that only provides a callback function as a Promise mode: (Take the off
The most recent way to implement mock virtual data through node is to use the FS module of node to read and write to our local files to achieve a mock function:
We can develop a JS file that specializes in encapsulating a simple file-reading function.
The JS code is as follows:
Let FS = require (' FS '); Let DEALFN = {/** * [write Data via promise] * @param {String} file [FileName] * @param {object} obj [written data (object)] * @return {Object}
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.