[![Build Status](https://secure.travis-ci.org/kriskowal/q.png?branch=master)](http://travis-ci.org/kriskowal/q)
<a href="http://promises-aplus.github.com/promises-spec">
<img src="http://kriskowal.github.io/q/q.png"
align="right" alt="Q logo" />
</a>
*This is Q version 1, from the `v1` branch in Git. This documentation applies to
the latest of both the version 1 and version 0.9 release trains. These releases
are stable. There will be no further releases of 0.9 after 0.9.7 which is nearly
equivalent to version 1.0.0. All further releases of `q@~1.0` will be backward
compatible. The version 2 release train introduces significant and
backward-incompatible changes and is experimental at this time.*
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.0.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 hand
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
NativeCallJS Cordova插件的Demo程序,因为太大传不上去Github,所以放到CSDN以便大家下载,项目地址 : https://github.com/15029352778/Cordova-NativeCallJS , 最新版增加向JS代码传参的功能
资源推荐
资源详情
资源评论
收起资源包目录
NativeCallJS Cordova插件 Demo程序--2016-8-27更新新版 (1971个子文件)
CordovaLib-debug.aar 112KB
android_sdk_version 1KB
resources-debug.ap_ 3.54MB
resources-debug-androidTest.ap_ 791B
resources-debug-androidTest.ap_ 759B
android-debug.apk 3.74MB
android-debug-unaligned.apk 3.74MB
AUTHORS 332B
gradlew.bat 2KB
list-started-emulators.bat 1KB
list-emulator-images.bat 1KB
install-emulator.bat 1KB
install-device.bat 1KB
start-emulator.bat 1KB
list-devices.bat 1KB
check_reqs.bat 1021B
version.bat 1015B
build.bat 1010B
clean.bat 1010B
run.bat 1006B
log.bat 1006B
fileSnapshots.bin 1.55MB
taskArtifacts.bin 122KB
fileHashes.bin 84KB
localClassSetAnalysis.bin 46KB
localClassSetAnalysis.bin 45KB
outputFileStates.bin 21KB
localJarClasspathSnapshot.bin 18KB
localJarClasspathSnapshot.bin 18KB
range.bnf 623B
.bowerrc 29B
iTunes-small.bplist 24KB
utf16.bplist 1KB
sample1.bplist 605B
sample2.bplist 384B
uid.bplist 365B
airplay.bplist 341B
int64.bplist 84B
build 2KB
changelog 496B
check_reqs 1KB
SystemWebView.class 53KB
CordovaActivity.class 41KB
MainActivity.class 31KB
CordovaWebViewImpl.class 21KB
PluginManager.class 17KB
SystemWebChromeClient.class 17KB
CordovaResourceApi.class 17KB
CordovaWebViewImpl.class 16KB
CordovaWebViewImpl.class 16KB
CordovaWebViewImpl.class 16KB
SystemWebViewEngine.class 15KB
SplashScreen.class 15KB
SystemWebViewClient.class 15KB
CoreAndroid.class 14KB
CordovaResourceApi.class 12KB
CordovaResourceApi.class 12KB
CordovaResourceApi.class 12KB
PluginResult.class 12KB
CordovaPlugin.class 12KB
PluginManager.class 12KB
PluginManager.class 12KB
PluginManager.class 12KB
CordovaActivity.class 11KB
CordovaActivity.class 11KB
CordovaActivity.class 11KB
SystemWebViewEngine.class 11KB
SystemWebViewEngine.class 11KB
SystemWebViewEngine.class 11KB
CordovaInterfaceImpl.class 11KB
WhitelistPlugin.class 11KB
SystemWebChromeClient.class 11KB
SystemWebChromeClient.class 11KB
SystemWebChromeClient.class 11KB
NativeToJsMessageQueue.class 10KB
NCJServer.class 10KB
StatusBar.class 10KB
Device.class 10KB
SystemWebViewClient.class 9KB
SystemWebViewClient.class 9KB
SystemWebViewClient.class 9KB
NativeCallJS.class 9KB
CordovaBridge.class 8KB
SplashScreen.class 8KB
SplashScreen.class 8KB
CoreAndroid.class 8KB
CoreAndroid.class 8KB
CoreAndroid.class 8KB
CordovaInterfaceImpl.class 7KB
CordovaInterfaceImpl.class 7KB
CordovaInterfaceImpl.class 7KB
CordovaWebViewImpl$EngineClient.class 7KB
NativeToJsMessageQueue.class 7KB
NativeToJsMessageQueue.class 7KB
NativeToJsMessageQueue.class 7KB
ConfigXmlParser.class 7KB
SystemWebChromeClient$5.class 7KB
SystemWebChromeClient$4.class 7KB
IonicKeyboard.class 7KB
CordovaPlugin.class 7KB
共 1971 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- yueguangkai0012017-08-14啥东东,完全没啥用
泰罗凹凸曼_
- 粉丝: 4
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- AIS2024 valid
- 最入门的爬虫代码 python.docx
- 爬虫零基础入门-爬取天气预报.pdf
- 最通俗易懂的 MongoDB 非结构化文档存储数据库教程.zip
- 以mongodb为数据库的订单物流小项目.zip
- 腾讯云-mongodb数据库, 项目部署.zip
- 腾讯 APIJSON 的 MongoDB 数据库插件.zip
- 理解非关系型数据库和关系型数据库的区别.zip
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功