Skip to content

Commit

Permalink
use ES.next code in the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Jun 10, 2015
1 parent fcdbc0b commit 91a343e
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions README.md
Expand Up @@ -2,7 +2,7 @@

[![NPM version](https://img.shields.io/npm/v/wrap-promise.svg)](https://www.npmjs.com/package/wrap-promise)
[![Bower version](https://img.shields.io/bower/v/wrap-promise.svg)](https://github.com/shinnn/wrap-promise/releases)
[![Build Status](https://img.shields.io/travis/shinnn/wrap-promise.svg)](https://travis-ci.org/shinnn/wrap-promise)
[![Build Status](https://travis-ci.org/shinnn/wrap-promise.svg?branch=master)](https://travis-ci.org/shinnn/wrap-promise)
[![Build status](https://ci.appveyor.com/api/projects/status/hs2fbpxk34gbteub?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/wrap-promise)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/wrap-promise.svg?label=cov)](https://coveralls.io/r/shinnn/wrap-promise)
[![Dependency Status](https://img.shields.io/david/shinnn/wrap-promise.svg?label=deps)](https://david-dm.org/shinnn/wrap-promise)
Expand All @@ -15,44 +15,36 @@ Like `new Promise()`, but prevents implicit rejection
### Using the native `new Promise()`

```javascript
var fs = require('fs');
const fs = require('fs');

new Promise(function(resolve, reject) {
new Promise((resolve, reject) => {
// Node's fs.readFile throws a type error when the first argument is not a string.

fs.readFile(123, function(err, buf) { // doesn't throw, but calls `onRejected` function
fs.readFile(123, (err, buf) => { // doesn't throw, but calls `onRejected` function
if (err) {
reject(err);
return;
}
resolve(buf);
});
}).catch(onRejected);

function onRejected() {
console.log('This function should be called.');
}
}).catch(() => console.log('This function should be called.'));
```

### Using *wrap-promise*

```javascript
var fs = require('fs');
var wrapPromise = require('wrap-promise');
const fs = require('fs');
const wrapPromise = require('wrap-promise');

wrapPromise(function(resolve, reject) {
fs.readFile(123, function(err, buf) { // doesn't call `onRejected` but throws immediately
wrapPromise((resolve, reject) => {
fs.readFile(123, (err, buf) => { // doesn't call `onRejected` but throws immediately
if (err) {
reject(err);
return;
}
resolve(buf);
});
}).catch(onRejected);

function onRejected() {
console.log('This function should not be called.');
}
}).catch(() => console.log('This function should not be called.'));
```

[According to the Promise specification](https://github.com/domenic/promises-unwrapping/blob/2a942729249c2490507a1ae6c9a24f8fa11a98e4/reference-implementation/lib/testable-implementation.js#L293-L297), a `promise` will [be rejected implicitly when an error is thrown in the constructor callback](http://www.html5rocks.com/en/tutorials/es6/promises/#toc-exceptions-and-promises). The only (and the biggest) difference is that *wrap-promise* immediately throws an error in such a case.
Expand All @@ -76,7 +68,7 @@ bower install wrap-promise
#### [Duo](http://duojs.org/)

```javascript
var wrapPromise = require('shinnn/wrap-promise');
const wrapPromise = require('shinnn/wrap-promise');
```

### Standalone
Expand All @@ -88,7 +80,7 @@ var wrapPromise = require('shinnn/wrap-promise');
### wrapPromise(*fn*)

*fn*: `Function`
Return: `Object` ([Promise](http://promisesaplus.com/))
Return: `Object` ([Promise](https://promisesaplus.com/))

It can be used in the same way as `new Promise()` but [`new` operator](http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.2) is not needed.

Expand All @@ -106,7 +98,7 @@ By default it uses the global `Promise` constructor if available, otherwise it [
If you don't need the fallback, use [`no-fallback.js`](https://github.com/shinnn/wrap-promise/blob/master/no-fallback.js) instead. (Useful for [Browserify](http://browserify.org/))

```javascript
var wrapPromise = require('wrap-promise/no-fallback');
const wrapPromise = require('wrap-promise/no-fallback');
```

#### On non-CommonJS environment
Expand Down

0 comments on commit 91a343e

Please sign in to comment.