Implement the promises library of nodejs Based on promise. js, promise. jsnodejs
Today, I downloaded promise. js from the GIT source code library and found that the source code is written based on Web Front-end JavaScript and cannot be directly used for nodejs. Fortunately,
The previous period of work, because the project to the front-end implementation of storage, so the use of Websql, and Websql API involves a lot of asynchronous problems, if the way to take the callback function, the code is not elegant, and is not conducive to understanding, so they found the promise, After the use of some of their own understanding and experience, to share with you in this article.What is promis
In node. js, q. js is used to implement api promise. node. jsq. js
About what is promise and promise solution is what problem, please experience node callback asynchronous encoding method, along with the step http://wiki.commonjs.
This article mainly introduces node. use q. js implements api promise. promise is a standard that describes the returned results of asynchronous calls, including correct returned results and error handling, need a friend can refer to what is promise and promise solution is w
This article describes how to use Promise in Node. js to encapsulate asynchronous functions and use unified chained APIs to get rid of the nightmare of multiple callbacks. it is a very practical little skill, I hope my friends will like writing Node. during the js process, continuous IO operations may lead to "pyramid nightmare". the multiple nesting of callback
functions are called, in addition, a promise can even accept a new callback after the implementation is resolved. These callbacks are done in a normal way, allowing us to use callbacks to implement a simple form of caching:vartaskspromise;functiongettasks () {taskpromise= Taskpromise | |Gettasksfromtheserver (); returnTaskpromise;} In this case, the Gettasks () function can be called any number of times, and it always returns the
PromiseThe motives for studying Promise are broadly as follows:
Unfamiliar with its API and curiosity about the implementation mechanism;
Many libraries (such as fetch) are based on the Promise package, so to understand the preconditions of these libraries first familiar with Promise;
To understand other more advanced asynchronous operations to b
It's enough to talk about js promise. It's enough to talk about jspromise.
I. background
We all know that nodejs is very fast. Why is it so fast? The reason is that node uses asynchronous callback to process the events that need to be waited, so that the Code will continue to be executed without waiting somewhere. But there is also a bad thing. When we have a lot of callbacks, for example, after the callbac
second parameter of then, used to specify the callback of the reject, the usage is this:GetNumber (). Then (function (data) { Console.log (' resolved '); Console.log (data);}). catch (function (reason) { console.log (' rejected '); Console.log (reason);});The effect is the same as the second parameter written in then. However, it has another function: When executing the resolve callback (that is, the first argument in the above then), if the exception is thrown (the code is wrong),
* Promise Commitment* Solve the problem of asynchronous programming in JS** Asynchronous-synchronous* Blocking-no blocking** What is the difference between synchronous and asynchronous? Synchronous refers to the mechanism by which the requested person (the handler of the thing) is notified when it is done. Async: When the event is completed, the requestor sends a message informing the requestor that the tra
of the Resolve method do not have a specific rule, basically put the parameters to be passed to the callback function in it. (then the method can receive this parameter value)6.promise.resolveIn general we will use new Promise () to create Promise objects, but we can use other methods as well. Here promise.resolve is used for example: New Promise (function (Reso
Use q. js in node. js to implement api promise
Here we will look at how to use q. js to implement node api promise.
I. Everything starts with install
The Code is as follows:
Npm install q
Ii. Standard node style api promise Me
Mode", these 4 methods can write more reasonable structure, better performance, maintenance more convenient JS code. These four modes are:First, the callback function (callback);Second, event monitoring (Listener);Third, the observer model;Iv. Promise ObjectsFirst, callback function (callback)The most basic method of asyncSuppose there are two functions, F1 and F2, which wait for the former to execute the
Deep understanding of js promise chain and promisechain
Native Promise is added to the new standard.
Here we will only discuss the use of the chain, and think about the details.
I. Review of then () and catch ()
The callback function can be placed in the parameters of then () and catch () to receive the final result of a Prom
When using Promise, our simplest understanding and usage is to provide the asynchronous result to the resolve as a parameter, and then pass a custom function to the then method as the result handler function, as in the code above. But what exactly are the two parameters of resolve and reject? Behind this, what is the basic way of doing it? Let's get to know it from a normative point of view.
New Promise ((
graph is given, resolve or reject, all of which are given by themselves. The goal is to launch the final state of P2 and P3.
These are four separate examples, and there is no link between them.
X means that P3 does not use then or catch functions, and does not talk about resolve or reject processing.
Finally, if there is a mistake, please correct me, thank you! ~!! @~~~
The above in-depth understanding of JS
The use of promise is excellent, it is so useful that I think we should study the promise, even the implementation of a simple version. Before we do that, let's take a look at the uses of promise:
Using PromiseCallback HellThe first use of promise is a good solution to the problem of callback black holes, assuming tha
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
-- Address: http://blog.chrisyip.im/nodejs-101-package-promise-and-async
First, let's review the Sagase project structure:
lib/ cli.js sagase.jsGruntfile.jspackage.json
I spoke about it in the previous article.package.json, This articlelib/sagase.js.
Because the code is relatively long, we will talk about it separately in one section. For the complete code, open GitHub.
'use strict';
Notifies the compiler to enter strict mode. The main function
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
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.