Есть функция, но я не могу вернуть аккумулированный результат. Где бы я не резолвил, то мне возвращается только результат первой итерации (в лучшем случае, иначе вообще undefined)
function resizeAndOptimizeImage(result, id) {
return new Promise((resolve, reject) => {
let array = []
Promise.all(
[
{width: 800, height: 600},
{width: 300, height: 100},
{width: 600, height: 200}
].map(image => {
image.slug = result.slug
let fullPath = `uploads/${image.width}x${image.height}-${image.slug}.${extention}`
sharp(req.file.buffer)
.resize(image.width, image.height)
.toFile(fullPath)
.then(result => {
Promise.all([
imagemin([fullPath], 'uploads', {
plugins: [
mozjpeg({quality: 80, progressive: true}),
pngquant({quality: '65-80'})
]
})
.then(result => {
const jpegpng = result[0].path.substr(result[0].path.lastIndexOf('/') + 1)
console.log('image optimized!')
array.push(jpegpng)
return jpegpng
})
.catch(err => reject(err)),
imagemin([fullPath], 'uploads', {
plugins: [
webp({quality: 80})
]
})
.then(result => {
const webp = result[0].path.substr(result[0].path.lastIndexOf('/') + 1)
console.log('image optimized!')
array.push(webp)
return webp
})
.catch(err => reject(err))
])
.then(result => {
console.log('RESULT1', result)
console.log('ARRAYFINAL', array)
resolve(array)
})
})
.then(result => {
console.log('RESULT3', result) // undefined
})
.catch(err => reject(err))
}))
.then(result => {
console.log('RESULT2', result) // undefined
})
})
}
Как вернуть результат всех итераций? В итоге должно быть 6 результатов (3 итерации в первом промис.олл и по 2 во втором).