[![Build Status](https://secure.travis-ci.org/kriskowal/q.svg?branch=master)](http://travis-ci.org/kriskowal/q)
[![CDNJS](https://img.shields.io/cdnjs/v/q.js.svg)](https://cdnjs.com/libraries/q.js)
<a href="http://promises-aplus.github.com/promises-spec">
<img src="http://kriskowal.github.io/q/q.png" align="right" alt="Q logo" />
</a>
If a function cannot return a value or throw an exception without
blocking, it can return a promise instead. A promise is an object
that represents the return value or the thrown exception that the
function may eventually provide. A promise can also be used as a
proxy for a [remote object][Q-Connection] to overcome latency.
[Q-Connection]: https://github.com/kriskowal/q-connection
On the first pass, promises can mitigate the “[Pyramid of
Doom][POD]”: the situation where code marches to the right faster
than it marches forward.
[POD]: http://calculist.org/blog/2011/12/14/why-coroutines-wont-work-on-the-web/
```javascript
step1(function (value1) {
step2(value1, function(value2) {
step3(value2, function(value3) {
step4(value3, function(value4) {
// Do something with value4
});
});
});
});
```
With a promise library, you can flatten the pyramid.
```javascript
Q.fcall(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.then(function (value4) {
// Do something with value4
})
.catch(function (error) {
// Handle any error from all above steps
})
.done();
```
With this approach, you also get implicit error propagation, just like `try`,
`catch`, and `finally`. An error in `promisedStep1` will flow all the way to
the `catch` function, where it’s caught and handled. (Here `promisedStepN` is
a version of `stepN` that returns a promise.)
The callback approach is called an “inversion of control”.
A function that accepts a callback instead of a return value
is saying, “Don’t call me, I’ll call you.”. Promises
[un-invert][IOC] the inversion, cleanly separating the input
arguments from control flow arguments. This simplifies the
use and creation of API’s, particularly variadic,
rest and spread arguments.
[IOC]: http://www.slideshare.net/domenicdenicola/callbacks-promises-and-coroutines-oh-my-the-evolution-of-asynchronicity-in-javascript
## Getting Started
The Q module can be loaded as:
- A ``<script>`` tag (creating a ``Q`` global variable): ~2.5 KB minified and
gzipped.
- A Node.js and CommonJS module, available in [npm](https://npmjs.org/) as
the [q](https://npmjs.org/package/q) package
- An AMD module
- A [component](https://github.com/component/component) as ``microjs/q``
- Using [bower](http://bower.io/) as `q#^1.4.1`
- Using [NuGet](http://nuget.org/) as [Q](https://nuget.org/packages/q)
Q can exchange promises with jQuery, Dojo, When.js, WinJS, and more.
## Resources
Our [wiki][] contains a number of useful resources, including:
- A method-by-method [Q API reference][reference].
- A growing [examples gallery][examples], showing how Q can be used to make
everything better. From XHR to database access to accessing the Flickr API,
Q is there for you.
- There are many libraries that produce and consume Q promises for everything
from file system/database access or RPC to templating. For a list of some of
the more popular ones, see [Libraries][].
- If you want materials that introduce the promise concept generally, and the
below tutorial isn't doing it for you, check out our collection of
[presentations, blog posts, and podcasts][resources].
- A guide for those [coming from jQuery's `$.Deferred`][jquery].
We'd also love to have you join the Q-Continuum [mailing list][].
[wiki]: https://github.com/kriskowal/q/wiki
[reference]: https://github.com/kriskowal/q/wiki/API-Reference
[examples]: https://github.com/kriskowal/q/wiki/Examples-Gallery
[Libraries]: https://github.com/kriskowal/q/wiki/Libraries
[resources]: https://github.com/kriskowal/q/wiki/General-Promise-Resources
[jquery]: https://github.com/kriskowal/q/wiki/Coming-from-jQuery
[mailing list]: https://groups.google.com/forum/#!forum/q-continuum
## Tutorial
Promises have a ``then`` method, which you can use to get the eventual
return value (fulfillment) or thrown exception (rejection).
```javascript
promiseMeSomething()
.then(function (value) {
}, function (reason) {
});
```
If ``promiseMeSomething`` returns a promise that gets fulfilled later
with a return value, the first function (the fulfillment handler) will be
called with the value. However, if the ``promiseMeSomething`` function
gets rejected later by a thrown exception, the second function (the
rejection handler) will be called with the exception.
Note that resolution of a promise is always asynchronous: that is, the
fulfillment or rejection handler will always be called in the next turn of the
event loop (i.e. `process.nextTick` in Node). This gives you a nice
guarantee when mentally tracing the flow of your code, namely that
``then`` will always return before either handler is executed.
In this tutorial, we begin with how to consume and work with promises. We'll
talk about how to create them, and thus create functions like
`promiseMeSomething` that return promises, [below](#the-beginning).
### Propagation
The ``then`` method returns a promise, which in this example, I’m
assigning to ``outputPromise``.
```javascript
var outputPromise = getInputPromise()
.then(function (input) {
}, function (reason) {
});
```
The ``outputPromise`` variable becomes a new promise for the return
value of either handler. Since a function can only either return a
value or throw an exception, only one handler will ever be called and it
will be responsible for resolving ``outputPromise``.
- If you return a value in a handler, ``outputPromise`` will get
fulfilled.
- If you throw an exception in a handler, ``outputPromise`` will get
rejected.
- If you return a **promise** in a handler, ``outputPromise`` will
“become” that promise. Being able to become a new promise is useful
for managing delays, combining results, or recovering from errors.
If the ``getInputPromise()`` promise gets rejected and you omit the
rejection handler, the **error** will go to ``outputPromise``:
```javascript
var outputPromise = getInputPromise()
.then(function (value) {
});
```
If the input promise gets fulfilled and you omit the fulfillment handler, the
**value** will go to ``outputPromise``:
```javascript
var outputPromise = getInputPromise()
.then(null, function (error) {
});
```
Q promises provide a ``fail`` shorthand for ``then`` when you are only
interested in handling the error:
```javascript
var outputPromise = getInputPromise()
.fail(function (error) {
});
```
If you are writing JavaScript for modern engines only or using
CoffeeScript, you may use `catch` instead of `fail`.
Promises also have a ``fin`` function that is like a ``finally`` clause.
The final handler gets called, with no arguments, when the promise
returned by ``getInputPromise()`` either returns a value or throws an
error. The value returned or error thrown by ``getInputPromise()``
passes directly to ``outputPromise`` unless the final handler fails, and
may be delayed if the final handler returns a promise.
```javascript
var outputPromise = getInputPromise()
.fin(function () {
// close files, database connections, stop servers, conclude tests
});
```
- If the handler returns a value, the value is ignored
- If the handler throws an error, the error passes to ``outputPromise``
- If the handler returns a promise, ``outputPromise`` gets postponed. The
eventual value or error has the same effect as an immediate return
value or thrown error: a value would be ignored, an error would be
forwarded.
If you are writing JavaScript for modern engines only or using
CoffeeScript, you may use `finally` instead of `fin`.
### Chaining
There are two ways to ch
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
如果您下载了本程序,但是该程序存在问题无法运行,那么您可以选择退款或者寻求我们的帮助(如果找我们帮助的话,是需要追加额外费用的)。另外,您不会使用资源的话(这种情况不支持退款),也可以找我们帮助(需要追加额外费用) 随着移动互联网技术的发展和用户需求的变化,【小程序名称】应运而生,以其轻量化、便捷化的设计理念为用户提供了一种全新的服务模式。作为一款无需下载安装即可使用的应用,【小程序名称】依托于微信庞大的生态系统,让用户在微信内就能轻松实现各种功能操作。 【小程序名称】的核心功能主要集中在【具体服务领域】,例如在线购物、本地生活服务、教育学习或健康管理等。它简化了传统APP繁琐的注册登录流程,支持微信一键授权登录,极大地提升了用户体验。用户通过搜索或扫描二维码,瞬间即可开启使用,享受快速加载、流畅运行的服务。 该小程序界面设计简洁明了,布局合理,易于上手。同时,其特色功能如实时更新的信息推送、个性化推荐以及社交分享功能,让用户能够及时获取所需信息,并方便地将优质内容分享至朋友圈或好友,实现信息的高效传播与互动。 【小程序名称】注重数据安全与隐私保护,严格遵守国家法律法规和微信平台的规定,确保用户数据的安全无虞。此外,其背后的开发团队持续迭代更新,根据用户反馈不断优化产品性能,提升服务质量,致力于打造一个贴近用户需求、充满活力的小程序生态。 总结来说,【小程序名称】凭借其小巧便携、快捷高效的特性,不仅节省了用户的手机存储空间,更为用户提供了无缝衔接的便利服务,是现代生活中不可或缺的一部分,真正实现了“触手可及”的智能生活新体验。只需轻点屏幕,无限精彩尽在掌握之中。
资源推荐
资源详情
资源评论
收起资源包目录
一套完整的社群电商系统,后端Api、后台管理系统、前台h5系统、小程序和App.zip (2000个子文件)
.babelrc 230B
app.45aa8aa04084f84284f19556739652fb.css 367KB
app.45aa8aa04084f84284f19556739652fb.css 367KB
app.45aa8aa04084f84284f19556739652fb.css 367KB
skin.min.css 42KB
visualblocks.css 5KB
content.min.css 4KB
content.inline.min.css 3KB
prism.css 2KB
.editorconfig 147B
tinymce.eot 18KB
tinymce-small.eot 9KB
gif_404.gif 1.02MB
loader.gif 3KB
smiley-cool.gif 354B
smiley-wink.gif 350B
smiley-smile.gif 344B
smiley-laughing.gif 343B
smiley-foot-in-mouth.gif 342B
smiley-frown.gif 340B
smiley-surprised.gif 338B
smiley-kiss.gif 338B
smiley-undecided.gif 337B
smiley-yell.gif 336B
smiley-innocent.gif 336B
smiley-embarassed.gif 331B
smiley-cry.gif 329B
smiley-tongue-out.gif 328B
smiley-sealed.gif 323B
smiley-money-mouth.gif 321B
object.gif 152B
anchor.gif 53B
trans.gif 43B
.gitignore 268B
.gitignore 268B
.gitignore 154B
.gitignore 67B
.gitkeep 0B
index.html 1KB
index.html 1KB
index.html 1KB
index.html 1KB
index.html 345B
index.html 341B
test.html 138B
OmsOrderExample.java 105KB
PmsProductExample.java 89KB
OmsOrderReturnApplyExample.java 67KB
OmsOrderItemExample.java 59KB
OmsCartItemExample.java 52KB
UmsMemberExample.java 48KB
SmsCouponExample.java 42KB
UmsMemberStatisticsInfoExample.java 40KB
CmsSubjectExample.java 38KB
PmsCommentExample.java 36KB
UmsMemberLevelExample.java 35KB
PmsSkuStockExample.java 32KB
PmsProductOperateLogExample.java 31KB
PmsProductAttributeExample.java 30KB
SmsHomeAdvertiseExample.java 29KB
CmsTopicExample.java 28KB
SmsCouponHistoryExample.java 28KB
PmsProductCategoryExample.java 28KB
OmsPortalOrderServiceImpl.java 28KB
UmsMemberReceiveAddressExample.java 27KB
OmsCompanyAddressExample.java 27KB
PmsBrandExample.java 27KB
UmsAdminExample.java 26KB
UmsPermissionExample.java 25KB
SmsFlashPromotionProductRelationExample.java 23KB
CmsMemberReportExample.java 23KB
UmsIntegrationChangeHistoryExample.java 22KB
UmsGrowthChangeHistoryExample.java 22KB
PmsFeightTemplateExample.java 22KB
CordovaWebViewImpl.java 21KB
NativeToJsMessageQueue.java 21KB
CmsSubjectCommentExample.java 20KB
CmsTopicCommentExample.java 20KB
UmsMemberRuleSettingExample.java 20KB
PmsCommentReplayExample.java 20KB
SmsFlashPromotionLogExample.java 20KB
UmsMemberLoginLogExample.java 20KB
CmsHelpExample.java 19KB
UmsRoleExample.java 19KB
SmsFlashPromotionExample.java 19KB
SmsFlashPromotionSessionExample.java 19KB
PluginManager.java 18KB
CordovaResourceApi.java 18KB
OmsOrderSettingExample.java 18KB
CordovaActivity.java 18KB
PmsAlbumExample.java 18KB
UmsAdminLoginLogExample.java 18KB
OmsOrderOperateHistoryExample.java 18KB
PmsProductVertifyRecordExample.java 18KB
CmsSubjectCategoryExample.java 17KB
CmsTopicCategoryExample.java 17KB
CmsHelpCategoryExample.java 17KB
SmsCouponProductCategoryRelationExample.java 17KB
PmsMemberPriceExample.java 16KB
UmsIntegrationConsumeSettingExample.java 16KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
JJJ69
- 粉丝: 6223
- 资源: 5780
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Vue的medical-vue医疗挂号系统设计源码
- Python解析网页.xmind
- PMV185XN-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- PMV170UN-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 基于Java的长理教务管理系统设计源码
- PMV16UN-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 永磁同步电机-用STM32F103C8T6实现PMSM矢量控制.rar
- PMV160UP-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 3dmax超级弯曲 暴力弯曲插件
- candence原理图批量换网络的快捷操作
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功