From af65adacfd9dfc044a84f26b0ac1e1c6f5cdfa87 Mon Sep 17 00:00:00 2001 From: wmzy <1256573276@qq.com> Date: Fri, 21 Dec 2018 21:32:03 +0800 Subject: [PATCH] Remove redundant promise constructor (#25) --- index.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 00bcb70..358231c 100644 --- a/index.js +++ b/index.js @@ -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})) :