<a href="http://promisesaplus.com/">
<img src="http://promisesaplus.com/assets/logo-small.png" alt="Promises/A+ logo"
title="Promises/A+ 1.1 compliant" align="right" />
</a>
[![Build Status](https://travis-ci.org/petkaantonov/bluebird.svg?branch=master)](https://travis-ci.org/petkaantonov/bluebird)
[![coverage-98%](http://img.shields.io/badge/coverage-98%-brightgreen.svg?style=flat)](http://petkaantonov.github.io/bluebird/coverage/debug/index.html)
**Got a question?** Join us on [stackoverflow](http://stackoverflow.com/questions/tagged/bluebird), the [mailing list](https://groups.google.com/forum/#!forum/bluebird-js) or chat on [IRC](https://webchat.freenode.net/?channels=#promises)
# Introduction
Bluebird is a fully featured [promise](#what-are-promises-and-why-should-i-use-them) library with focus on innovative features and performance
# Topics
- [Features](#features)
- [Quick start](#quick-start)
- [API Reference and examples](API.md)
- [Support](#support)
- [What are promises and why should I use them?](#what-are-promises-and-why-should-i-use-them)
- [Questions and issues](#questions-and-issues)
- [Error handling](#error-handling)
- [Development](#development)
- [Testing](#testing)
- [Benchmarking](#benchmarks)
- [Custom builds](#custom-builds)
- [For library authors](#for-library-authors)
- [What is the sync build?](#what-is-the-sync-build)
- [License](#license)
- [Snippets for common problems](https://github.com/petkaantonov/bluebird/wiki/Snippets)
- [Promise anti-patterns](https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns)
- [Changelog](changelog.md)
- [Optimization guide](#optimization-guide)
# Features
<img src="http://petkaantonov.github.io/bluebird/logo.png" alt="bluebird logo" align="right" />
- [Promises A+](http://promisesaplus.com)
- [Synchronous inspection](API.md#synchronous-inspection)
- [Concurrency coordination](API.md#collections)
- [Promisification on steroids](API.md#promisification)
- [Resource management through a parallel of python `with`/C# `using`](API.md#resource-management)
- [Cancellation and timeouts](API.md#cancellation)
- [Parallel for C# `async` and `await`](API.md#generators)
- Mind blowing utilities such as
- [`.bind()`](API.md#binddynamic-thisarg---promise)
- [`.call()`](API.md#callstring-propertyname--dynamic-arg---promise)
- [`Promise.join()`](API.md#promisejoinpromisethenablevalue-promises-function-handler---promise)
- [And](API.md#core) [much](API.md#timers) [more](API.md#utility)!
- [Practical debugging solutions and sane defaults](#error-handling)
- [Sick performance](benchmark/)
<hr>
# Quick start
## Node.js
npm install bluebird
Then:
```js
var Promise = require("bluebird");
```
## Browsers
There are many ways to use bluebird in browsers:
- Direct downloads
- Full build [bluebird.js](https://cdn.jsdelivr.net/bluebird/2.10.2/bluebird.js)
- Full build minified [bluebird.min.js](https://cdn.jsdelivr.net/bluebird/2.10.2/bluebird.min.js)
- You may use browserify on the main export
- You may use the [bower](http://bower.io) package.
When using script tags the global variables `Promise` and `P` (alias for `Promise`) become available.
A [minimal bluebird browser build](#custom-builds) is ≈38.92KB minified*, 11.65KB gzipped and has no external dependencies.
*Google Closure Compiler using Simple.
#### Browser support
Browsers that [implement ECMA-262, edition 3](http://en.wikipedia.org/wiki/Ecmascript#Implementations) and later are supported.
[![Selenium Test Status](https://saucelabs.com/browser-matrix/petka_antonov.svg)](https://saucelabs.com/u/petka_antonov)
**Note** that in ECMA-262, edition 3 (IE7, IE8 etc.) it is not possible to use methods that have keyword names like `.catch` and `.finally`. The [API documentation](API.md) always lists a compatible alternative name that you can use if you need to support these browsers. For example `.catch` is replaced with `.caught` and `.finally` with `.lastly`.
Also, [long stack trace](API.md#promiselongstacktraces---void) support is only available in Chrome, Firefox and Internet Explorer 10+.
After quick start, see [API Reference and examples](API.md)
<hr>
# Support
- Mailing list: [bluebird-js@googlegroups.com](https://groups.google.com/forum/#!forum/bluebird-js)
- IRC: #promises @freenode
- StackOverflow: [bluebird tag](http://stackoverflow.com/questions/tagged/bluebird)
- Bugs and feature requests: [github issue tracker](https://github.com/petkaantonov/bluebird/issues?state=open)
<hr>
# What are promises and why should I use them?
You should use promises to turn this:
```js
fs.readFile("file.json", function(err, val) {
if( err ) {
console.error("unable to read file");
}
else {
try {
val = JSON.parse(val);
console.log(val.success);
}
catch( e ) {
console.error("invalid json in file");
}
}
});
```
Into this:
```js
fs.readFileAsync("file.json").then(JSON.parse).then(function(val) {
console.log(val.success);
})
.catch(SyntaxError, function(e) {
console.error("invalid json in file");
})
.catch(function(e) {
console.error("unable to read file");
});
```
*If you are wondering "there is no `readFileAsync` method on `fs` that returns a promise", see [promisification](API.md#promisification)*
Actually you might notice the latter has a lot in common with code that would do the same using synchronous I/O:
```js
try {
var val = JSON.parse(fs.readFileSync("file.json"));
console.log(val.success);
}
//Syntax actually not supported in JS but drives the point
catch(SyntaxError e) {
console.error("invalid json in file");
}
catch(Error e) {
console.error("unable to read file");
}
```
And that is the point - being able to have something that is a lot like `return` and `throw` in synchronous code.
You can also use promises to improve code that was written with callback helpers:
```js
//Copyright Plato http://stackoverflow.com/a/19385911/995876
//CC BY-SA 2.5
mapSeries(URLs, function (URL, done) {
var options = {};
needle.get(URL, options, function (error, response, body) {
if (error) {
return done(error);
}
try {
var ret = JSON.parse(body);
return done(null, ret);
}
catch (e) {
done(e);
}
});
}, function (err, results) {
if (err) {
console.log(err);
} else {
console.log('All Needle requests successful');
// results is a 1 to 1 mapping in order of URLs > needle.body
processAndSaveAllInDB(results, function (err) {
if (err) {
return done(err);
}
console.log('All Needle requests saved');
done(null);
});
}
});
```
Is more pleasing to the eye when done with promises:
```js
Promise.promisifyAll(needle);
var options = {};
var current = Promise.resolve();
Promise.map(URLs, function(URL) {
current = current.then(function () {
return needle.getAsync(URL, options);
});
return current;
}).map(function(responseAndBody){
return JSON.parse(responseAndBody[1]);
}).then(function (results) {
return processAndSaveAllInDB(results);
}).then(function(){
console.log('All Needle requests saved');
}).catch(function (e) {
console.log(e);
});
```
Also promises don't just give you correspondences for synchronous features but can also be used as limited event emitters or callback aggregators.
More reading:
- [Promise nuggets](https://promise-nuggets.github.io/)
- [Why I am switching to promises](http://spion.github.io/posts/why-i-am-switching-to-promises.html)
- [What is the the point of promises](http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/#toc_1)
- [Snippets for common problems](https://github.com/petkaantonov/bluebird/wiki/Snippets)
- [Promise anti-patterns](https://github.com/petkaantonov
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Elasticsearch x-pack5.0 (2000个子文件)
font-awesome.css 32KB
font-awesome.min.css 26KB
style.css 12KB
select.css 8KB
common.css 8KB
docs.css 6KB
select.min.css 5KB
pdfmake.css 3KB
reporting-overrides.css 2KB
jquery-ui.min.css 2KB
examples.css 2KB
prettify.css 676B
lesshat.css 0B
index.html 28KB
index.html 25KB
index.html 25KB
index.html 23KB
index.html 21KB
index.js.html 21KB
index.html 20KB
index.html 18KB
index.html 12KB
index.html 12KB
settings.html 11KB
normalization_test.html 9KB
index.html 8KB
performance_test.html 8KB
index.html 7KB
edit_user.html 7KB
edit_role.html 6KB
index.html 5KB
index.html 5KB
demo-tagging.html 5KB
demo-multiple-selection.html 5KB
index.html 5KB
styled.html 4KB
test.html 4KB
index.html 4KB
demo-disable-search.html 4KB
demo-select2-with-bootstrap.html 4KB
index.html 4KB
index.html 4KB
index.html 4KB
demo-selectize-with-bootstrap.html 4KB
account.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
demo-append-to-body.html 3KB
index.html 3KB
demo-bootstrap.html 3KB
index.html 3KB
users.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
demo-object-as-source.html 3KB
index.html 3KB
demo-focus.html 3KB
boundingbox.html 3KB
index.html 3KB
demo-dropdown-position.html 3KB
jobs.html 3KB
sublabels.html 3KB
roles.html 2KB
dynamic.html 2KB
demo-group-by.html 2KB
_header.html 2KB
demo-basic.html 2KB
index.html 2KB
index.html 2KB
intersection_tooltip.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
interactive.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
demo-group-filter.html 2KB
weighted.html 2KB
index.html 2KB
index.html 2KB
login.html 2KB
index.html 2KB
index_template.html 1KB
node_template.html 1KB
save_workspace.html 1KB
select-multiple.tpl.html 1KB
orientation_order.html 1KB
kibana_template.html 1KB
demo-bind-to-single-property-async.html 1KB
demo-event-on-selection.html 1KB
index.html 1KB
select.tpl.html 1KB
demo-bind-to-single-property.html 1KB
index.html 1KB
venn_venn.html 1KB
overview_template.html 1KB
access_denied.html 1KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- yqianer2016-12-12官网上下载真是太慢了,这个对我来说真是太及时了
- zangjikun2016-11-24赞,国内下载确实蛋疼
- ruhe82016-11-25国外的下载慢的受不了
- renjianchouchangke882017-12-20哎,不怎么的
sunchenzl
- 粉丝: 9
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功