Обычно я делаю что-то вроде этого
createQueue(sqs, queueName)
.then(url => {
return enqueue(sqs, url, message)
})
.then(messageId => {
return res.status(HttpStatus.OK).send({ id: messageId })
})
.catch(err => {
return handleErr(res, HttpStatus.INTERNAL_SERVER_ERROR, err)
})
Но в этом случае у меня есть if
проверка, существует ли URL-адрес, и если нет, я хочу позвонить createQueue
, но в обоих случаях,Я хочу позвонить в остальную часть цепочки обещаний.
Каков наилучший способ сделать это?
// This doesn't work
if (!req.queueUrl) {
return createQueue(sqs, queueName)
}
.then(url => {
const myUrl = req.queueUrl || url
return enqueue(sqs, myUrl, message)
})
.then(messageId => {
return res.status(HttpStatus.OK).send({ id: messageId })
})
.catch(err => {
return handleErr(res, HttpStatus.INTERNAL_SERVER_ERROR, err)
})
Примечание Вот как я решил это в прошлом
const promises = []
if (!req.queueUrl) {
promises.push(createQueue(sqs, queueName))
}
Promise.all(promises)
.then(url => {
const myUrl = req.queueUrl || url
return enqueue(sqs, myUrl, message)
})
.then(messageId => {
return res.status(HttpStatus.OK).send({ id: messageId })
})
.catch(err => {
return handleErr(res, HttpStatus.INTERNAL_SERVER_ERROR, err)
})