Skip to content

Commit

Permalink
Minor tweak (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb authored Mar 10, 2021
1 parent e63f3be commit 7f1b233
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
13 changes: 5 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const pMap = require('p-map');
const pTimes = require('p-times');

const sum = iterable => {
let total = 0;
Expand Down Expand Up @@ -35,15 +35,14 @@ class PProgress extends Promise {

return PProgress.fn(progress => {
const progressMap = new Map();
const iterator = promises[Symbol.iterator]();

const reportProgress = () => {
progress(sum(progressMap) / promises.length);
};

const mapper = async () => {
const next = iterator.next().value;
const promise = typeof next === 'function' ? next() : next;
const mapper = async index => {
const nextValue = promises[index];
const promise = typeof nextValue === 'function' ? nextValue() : nextValue;
progressMap.set(promise, 0);

if (promise instanceof PProgress) {
Expand All @@ -59,9 +58,7 @@ class PProgress extends Promise {
return value;
};

// TODO: This is kinda ugly. Find a better way to do this.
// Maybe `p-map` could accept a number as the first argument?
return pMap(new Array(promises.length), mapper, options);
return pTimes(promises.length, mapper, options);
})();
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"bluebird"
],
"dependencies": {
"p-map": "^3.0.0"
"p-times": "^3.0.0"
},
"devDependencies": {
"ava": "^2.2.0",
Expand Down

0 comments on commit 7f1b233

Please sign in to comment.