1
我正在寻求一些算法建议。比方说,这个想法是你必须做这样的事情:批量操作一个接一个v.s批量操作
operation.getAnimal('zebra')
.then(animal => {
return operation.doSomethingWithAnimal(animal)
})
但不是这样一个只是时间,你需要为许多不同的动物的做到这一点。
let animals = ['zebra', 'dog', 'cat', 'fish', 'bird']
Promise.map(animals).then(animalName => {
return operation.getAnimal(animalName)
.then(animal => {
return operation.doSomethingWithAnimal(animal)
})
})
,或者你可以做这样的事情:
function props (items, promise) {
let results = {}
_.each(items, item => {
results[item] = promise(item)
})
return results
}
Promise.props(props(animals, option.getAnimal))
.then(gottenAnimals => {
return Promise.props(props(gottenAnimals, options.doSomethingWithAnimal))
})
第一个例子将得到动物,然后做一些事情,在这里, 第二个例子将得到所有的动物然后运行做一些与他们。
不是一大堆的差异。在第一个例子中,你应该保存一些内存,因为在对它们进行处理之前,你并没有将它们全部存储在数组中。 –
感谢一群@MikeC,这是我正在寻找的确切类型的东西。 – ThomasReggi