-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ab3a542
Showing
10 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage | ||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: node_js | ||
node_js: stable | ||
sudo: false | ||
|
||
after_success: | ||
- npm run-script codeclimate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# es5-async-await | ||
[![npm package](https://badge.fury.io/js/es5-async-await.svg)](http://badge.fury.io/js/es5-async-await) | ||
[![build](https://travis-ci.org/bakerface/es5-async-await.svg?branch=master)](https://travis-ci.org/bakerface/es5-async-await) | ||
[![code climate](https://codeclimate.com/github/bakerface/es5-async-await/badges/gpa.svg)](https://codeclimate.com/github/bakerface/es5-async-await) | ||
[![coverage](https://codeclimate.com/github/bakerface/es5-async-await/badges/coverage.svg)](https://codeclimate.com/github/bakerface/es5-async-await/coverage) | ||
[![issues](https://img.shields.io/github/issues/bakerface/es5-async-await.svg)](https://github.com/bakerface/es5-async-await/issues) | ||
[![dependencies](https://david-dm.org/bakerface/es5-async-await.svg)](https://david-dm.org/bakerface/es5-async-await) | ||
[![devDependencies](https://david-dm.org/bakerface/es5-async-await/dev-status.svg)](https://david-dm.org/bakerface/es5-async-await#info=devDependencies) | ||
[![downloads](http://img.shields.io/npm/dm/es5-async-await.svg)](https://www.npmjs.com/package/es5-async-await) | ||
|
||
#### Table of Contents | ||
[#](#) **async**(*fn*) - create an async function. | ||
<br> | ||
[#](#) **await**(*promise*) - wait for an async function to resolve. | ||
|
||
``` javascript | ||
var async = require('es5-async-await/async'); | ||
var await = require('es5-async-await/await'); | ||
|
||
function sleep(ms) { | ||
return new Promise(function(resolve) { | ||
setTimeout(resolve, ms); | ||
}); | ||
} | ||
|
||
function getMessageById(id) { | ||
if (id) return Promise.resolve('and you can await values'); | ||
return Promise.reject('using try/catch for errors'); | ||
} | ||
|
||
var example = async(function(ms) { | ||
console.log('you can pass async function arguments'); | ||
await(sleep(ms)); | ||
|
||
var message = await(getMessageById(1234)); | ||
console.log(message); | ||
|
||
try { | ||
await(getMessageById()); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
} | ||
|
||
return 'and promises are returned'; | ||
}); | ||
|
||
example(1000) | ||
.then(console.log) | ||
.catch(console.error); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var Promise = require('when').Promise; | ||
|
||
require('fibers/future'); | ||
|
||
module.exports = function(fn) { | ||
return function() { | ||
var _this = this; | ||
var _args = [].slice.call(arguments, 0); | ||
|
||
return new Promise(function(resolve, reject) { | ||
fn.future().apply(_this, _args).resolve(function(err, value) { | ||
if (err) { | ||
reject(err); | ||
} | ||
else { | ||
resolve(value); | ||
} | ||
}); | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var Future = require('fibers/future'); | ||
|
||
module.exports = function(promise) { | ||
var future = new Future(); | ||
|
||
promise | ||
.then(function(value) { | ||
future.return(value); | ||
}) | ||
.catch(function(error) { | ||
future.throw(error); | ||
}); | ||
|
||
return future.wait(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Copyright (c) 2016 Christopher M. Baker | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
var async = require('../async'); | ||
var await = require('../await'); | ||
var Promise = require('when').Promise; | ||
|
||
function sleep(ms) { | ||
return new Promise(function(resolve) { | ||
setTimeout(resolve, ms); | ||
}); | ||
} | ||
|
||
function getMessageById(id) { | ||
if (id) return Promise.resolve('and you can await values'); | ||
return Promise.reject('using try/catch for errors'); | ||
} | ||
|
||
var example = async(function(ms) { | ||
console.log('you can pass async function arguments'); | ||
await(sleep(ms)); | ||
|
||
var message = await(getMessageById(1234)); | ||
console.log(message); | ||
|
||
try { | ||
await(getMessageById()); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
} | ||
|
||
return 'and promises are returned'; | ||
}); | ||
|
||
example(1000) | ||
.then(console.log) | ||
.catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright (c) 2016 Christopher M. Baker | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
var fs = require('fs'); | ||
var async = require('../async'); | ||
var await = require('../await'); | ||
var when = require('when/node'); | ||
var readFile = when.lift(fs.readFile); | ||
|
||
var example = async(function() { | ||
var content = await(readFile(__dirname + '/../package.json')); | ||
var packageJson = JSON.parse(content); | ||
|
||
console.log('The name of this package is', packageJson.name); | ||
}); | ||
|
||
example(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Copyright (c) 2016 Christopher M. Baker | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
module.exports.async = require('./async'); | ||
module.exports.await = require('./await'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"name": "es5-async-await", | ||
"version": "1.0.0", | ||
"description": "Bringing async/await to ES5", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "xo && istanbul cover node_modules/.bin/_mocha", | ||
"codeclimate": "codeclimate-test-reporter < coverage/lcov.info" | ||
}, | ||
"xo": { | ||
"space": 2, | ||
"semicolon": true, | ||
"globals": [ | ||
"describe", | ||
"before", | ||
"beforeEach", | ||
"after", | ||
"afterEach", | ||
"it" | ||
], | ||
"ignores": [ | ||
"examples/*" | ||
], | ||
"rules": { | ||
"brace-style": [ | ||
2, | ||
"stroustrup" | ||
], | ||
"space-before-function-paren": [ | ||
2, | ||
"never" | ||
] | ||
} | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/bakerface/es5-async-await.git" | ||
}, | ||
"keywords": [], | ||
"author": "Christopher M. Baker", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/bakerface/es5-async-await/issues" | ||
}, | ||
"homepage": "https://github.com/bakerface/es5-async-await", | ||
"devDependencies": { | ||
"istanbul": "^0.4.2", | ||
"mocha": "^2.4.5", | ||
"should": "^8.2.2", | ||
"xo": "^0.13.0" | ||
}, | ||
"dependencies": { | ||
"fibers": "^1.0.10", | ||
"when": "^3.7.7" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright (c) 2016 Christopher M. Baker | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
var es5 = require('..'); | ||
var Promise = require('when').Promise; | ||
|
||
require('should'); | ||
|
||
function getUserById(id) { | ||
if (id) { | ||
return Promise.resolve({ | ||
id: id, | ||
username: 'bakerface' | ||
}); | ||
} | ||
|
||
return Promise.reject(new Error()); | ||
} | ||
|
||
var getUsernameById = es5.async(function(id) { | ||
var user = es5.await(getUserById(id)); | ||
return user.username; | ||
}); | ||
|
||
var getUsernameByIdSafe = es5.async(function(id) { | ||
try { | ||
var user = es5.await(getUserById(id)); | ||
return user.username; | ||
} | ||
catch (e) { | ||
return 'unknown'; | ||
} | ||
}); | ||
|
||
describe('promise', function() { | ||
it('should resolve when the promise resolves', function(done) { | ||
getUsernameById(1234).then(function(username) { | ||
username.should.eql('bakerface'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should reject when the promise rejects', function(done) { | ||
getUsernameById().catch(function() { | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should support try-catch blocks', function(done) { | ||
getUsernameByIdSafe().then(function(username) { | ||
username.should.eql('unknown'); | ||
done(); | ||
}); | ||
}); | ||
}); |