Skip to content

Commit

Permalink
Added a simple usage example (#1369)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiloif authored and benjamingr committed May 10, 2018
1 parent 39081ba commit 9159472
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/docs/api/promise.each.md
Expand Up @@ -37,6 +37,29 @@ Promise.each(fileNames, function(fileName) {
});
```

A simple usage example
```js
const Promise = require('bluebird');
const path = require('path');
const fs = Promise.promisifyAll(require('fs'));
let fileNames = ['a.txt','b.txt','c.txt','d.txt']

// All promises will be executed serrialy the next
// iteration will start only after the previous promise is fulfilled
return Promise.each(fileNames, file => {
return fs.readFileAsync(path.resolve(__dirname, file)).then(data => {
console.log(data.toString());
}) // If you use then or catch here it'll be called after each iteration


}).then(arr => { // Will be called after all promises are resolved
// arr will just include the original array
// so resolt hanle should be inside each iteration
}).catch(err => { // Get here if one of the promises was rejected and stop all others

})
```

<hr>
</markdown></div>

Expand Down

0 comments on commit 9159472

Please sign in to comment.