Skip to content

Commit

Permalink
Remove redundant promise constructor (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmzy authored and sindresorhus committed Dec 21, 2018
1 parent 0f42626 commit af65ada
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ const isAvailable = options => new Promise((resolve, reject) => {
});
});

const getPort = (options = {}) => new Promise((resolve, reject) => {
const getPort = (options = {}) => {
if (typeof options.port === 'number') {
options.port = [options.port];
}

options.port.reduce((seq, port) => {
return seq.catch(() => {
return isAvailable(Object.assign({}, options, {port}))
.then(port => port)
.catch(Promise.reject.bind(Promise));
});
}, Promise.reject()).then(resolve).catch(reject);
});
return (options.port || []).reduce(
(seq, port) => seq.catch(
() => isAvailable(Object.assign({}, options, {port}))
),
Promise.reject()
);
};

module.exports = options => options ?
getPort(options).catch(() => getPort({port: 0})) :
Expand Down

0 comments on commit af65ada

Please sign in to comment.