我正在使用bluebird Promise.map链接诺言。
promise本身是SVG文件的简单s3上传。
问题是,有时我在上传时遇到“套接字挂断”错误,这将停止整个过程。
这是功能
const createMockup = async product => new Promise ((resolve, reject) => {
const canvas = new fabric.StaticCanvas() // this doesn't matter
s3.putObject({
Bucket: 'myBucket',
Key: `Products/${product._id}/upload.svg`,
ContentType: 'image/svg+xml',
Body: canvas.toSVG(),
ACL: 'public-read',
}).promise().then(() => {
console.log('OK')
resolve()
}).catch((err) => {
console.log('KO 1')
reject(err)
})
}
这是应许链:
const results = Promise.map(products, item => createMockup(item).then(result => ({
result,
}), err => ({
err,
})), {
concurrency: 1,
})
const completed = results.filter(item => 'result' in item).map(item => item.result)
const errors = results.filter(item => 'error' in item).map(item => item.error)
completed.each((item) => {
console.log(`OK for ${item._id}`)
})
errors.each((item) => {
console.log(`KO for ${item._id}`)
})
如何防止这些“插座挂断”?或者至少,如何确保即使上载失败,脚本也可以直接到达下一个项目而不会卡在套接字错误上?