Comparing version 9.7.1 to 9.7.2
{ | ||
"name": "utilsac", | ||
"version": "9.7.1", | ||
"version": "9.7.2", | ||
"description": "Utility functions", | ||
@@ -5,0 +5,0 @@ "main": "files.js", |
(function () { | ||
const timePromise = function (promiseCreator) { | ||
// returns a Promise that resolves with | ||
// the time elapsed for the promise to resolve and its value | ||
// executes promiseCreator and waits for it to resolve | ||
return new Promise(function (resolve, reject) { | ||
const startTime = performance.now(); | ||
promiseCreator().then(function (value) { | ||
const endTime = performance.now(); | ||
resolve({ | ||
timeElapsed: endTime - startTime, | ||
value | ||
}); | ||
}).catch(reject); | ||
}); | ||
}; | ||
timePromise(() => fetch(`https://duckduckgo.com/lite`)).then(console.log) | ||
}()); | ||
(function () { | ||
const timePromise = function (promiseCreator) { | ||
/* returns a Promise that resolves with | ||
the time elapsed for the promise to resolve and its value | ||
executes promiseCreator and waits for it to resolve */ | ||
const timePromise = function (promiseCreator) { | ||
/* returns a Promise that resolves with | ||
the time elapsed for the promise to resolve and its value | ||
executes promiseCreator and waits for it to resolve */ | ||
const startTime = performance.now(); | ||
return promiseCreator().then(function (value) { | ||
const endTime = performance.now(); | ||
return { | ||
timeElapsed: endTime - startTime, | ||
value | ||
}; | ||
}); | ||
}; | ||
timePromise(() => fetch(`https://duckduckgo.com/lite`)).then(console.log) | ||
}()); | ||
const startTime = performance.now(); | ||
return promiseCreator().then(function (value) { | ||
const endTime = performance.now(); | ||
return { | ||
timeElapsed: endTime - startTime, | ||
value | ||
}; | ||
}); | ||
}; | ||
timePromise(() => fetch(`https://duckduckgo.com/lite`)).then(console.log) | ||
}()); | ||
@@ -149,17 +149,18 @@ export { | ||
const chainPromises = function (promiseCreators) { | ||
/* different than Promise.all | ||
only executes promiseCreator one after the previous has resolved | ||
resolves with an array of values */ | ||
/* different than Promise.all, takes an array of functions that return a promise | ||
only executes promiseCreators sequentially | ||
resolves with an array of values or reject with the first error*/ | ||
const length = promiseCreators.length; | ||
const values = []; | ||
if (length === 0) { | ||
return Promise.resolve(values); | ||
} | ||
let i = -1; | ||
return new Promise(function (resolve, reject) { | ||
let i = 0; | ||
const chainer = function (value) { | ||
i += 1; | ||
values.push(value); | ||
if (i > 0) { | ||
values.push(value); | ||
} | ||
if (i < length) { | ||
promiseCreators[i]().then(chainer); | ||
const promise = promiseCreators[i](); | ||
promise.then(chainer); | ||
promise.catch(reject); | ||
} else { | ||
@@ -169,3 +170,3 @@ resolve(values); | ||
}; | ||
promiseCreators[i]().then(chainer); | ||
chainer(); | ||
}); | ||
@@ -172,0 +173,0 @@ }; |
1
26957
556