node-powertools
Advanced tools
Comparing version
@@ -180,2 +180,8 @@ (function (root, factory) { | ||
Powertools.iterate = function(array, callback) { | ||
return array.reduce(function (promise, item, index) { | ||
return promise.then(function () {return callback(item, index, array)}); | ||
}, Promise.resolve()); | ||
}; | ||
// Helpers | ||
@@ -182,0 +188,0 @@ function getKeys(obj, prefix) { |
{ | ||
"name": "node-powertools", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"description": "Powerful assistive functions for Node and Browser environments.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -232,3 +232,3 @@ <p align="center"> | ||
### powertools.uniquify(arr) | ||
Return an array with all duplicates removed. | ||
Return an array `arr` with all duplicates removed. | ||
```js | ||
@@ -238,2 +238,22 @@ powertools.uniquify([{id: 1}, {id: 1}, {id: 2}]); // Output: [{id: 1}, {id: 2}] | ||
### powertools.iterate(arr, callback) | ||
Asynchronously iterate through an array `arr` and call `callback` on each element. This function is useful for when you need to wait for asynchronous functions to complete before moving on to the next element in the array. | ||
```js | ||
const sampleArray = [1, 2, 3, 4, 5]; | ||
// Simulate an async operation like a database call | ||
const asyncTask = (item, index) => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
console.log(`Processing item ${item} at index ${index}`); | ||
return resolve(); | ||
}, 1000); | ||
}); | ||
}; | ||
// Process each item in the array asynchronously | ||
Powertools.iterate(sampleArray, asyncTask) | ||
.then(() => console.log('All tasks completed.')); | ||
``` | ||
## Final Words | ||
@@ -240,0 +260,0 @@ If you are still having difficulty, we would love for you to post a question to [the Node Powertools issues page](https://github.com/itw-creative-works/node-powertools/issues). It is much easier to answer questions that include your code and relevant files! So if you can provide them, we'd be extremely grateful (and more likely to help you find the answer!) |
@@ -180,2 +180,8 @@ (function (root, factory) { | ||
Powertools.iterate = function(array, callback) { | ||
return array.reduce(function (promise, item, index) { | ||
return promise.then(function () {return callback(item, index, array)}); | ||
}, Promise.resolve()); | ||
}; | ||
// Helpers | ||
@@ -182,0 +188,0 @@ function getKeys(obj, prefix) { |
58943
2.02%1293
0.78%269
8.03%