Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES2015ify #16

Merged
merged 2 commits into from
Feb 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- '6'
- '4'
- '0.12'
47 changes: 24 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
'use strict';

function generate(willResolve) {
return function (ms, value) {
ms = ms || 0;
const generate = willResolve => function (ms, value) {
ms = ms || 0;

// If supplied, the thunk will override promise results with `value`.
var useValue = arguments.length > 1;
// If supplied, the thunk will override promise results with `value`.
const useValue = arguments.length > 1;

var promise = thunk(value);
thunk.then = promise.then.bind(promise);
thunk.catch = promise.catch.bind(promise);
thunk._actualPromise = promise;
return thunk;
let promise;

function thunk(result) {
if (promise) {
// Prevent unhandled rejection errors if promise is never used, and only used as a thunk
promise.catch(function () {});
}
return new Promise(function (resolve, reject) {
setTimeout(
willResolve ? resolve : reject,
ms,
useValue ? value : result
);
});
const thunk = result => {
if (promise) {
// Prevent unhandled rejection errors if promise is never used, and only used as a thunk
promise.catch(() => {});
}
return new Promise((resolve, reject) => {
setTimeout(
willResolve ? resolve : reject,
ms,
useValue ? value : result
);
});
};
}

promise = thunk(value);
thunk.then = promise.then.bind(promise);
thunk.catch = promise.catch.bind(promise);
thunk._actualPromise = promise;

return thunk;
};

module.exports = generate(true);
module.exports.reject = generate(false);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.12.0"
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
Expand Down