From 91a343e31f5c91d8089d9b84bd8365a05c3fa5e5 Mon Sep 17 00:00:00 2001 From: Shinnosuke Watanabe Date: Wed, 10 Jun 2015 16:17:48 +0900 Subject: [PATCH] use ES.next code in the readme --- README.md | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8de94b6..50a3c02 100755 --- a/README.md +++ b/README.md @@ -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) @@ -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. @@ -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 @@ -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. @@ -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