I have an array that contains filenames at each index. I want to download those files one at a time (synchronously). I know about 'Async
' module. But I want to know whether any functions in Lodash
or Underscore
or Bluebird
library supports this functionality.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Keeping track of variable instances
using Bluebird, there is a situation similar to yours with an answer here: How to chain a variable number of promises in Q, in order?
This seems like a decent solution but in my opinion much less readable and elegant as async.eachSeries(I know you said you don`t want the 'async' solution but maybe you can reconsider.
You can use bluebird's
Promise.mapSeries
:Depending on what you use to download file, you may have to build a promise or not.
Update 1
An exemple of a
downloadFile()
function using only nodejs:Update 2
As Gorgi Kosev suggested, building a chain of promises using a loop works too:
A chain of promise will get you only the result of the last promise in the chain while
mapSeries()
gives you an array with the result of each promise.