我有一个服务器端点,我想返回异步请求的结果或承诺拒绝Promise.reject('error message')
,但是当我将其添加Promise.reject
到函数而不是仅返回异步请求时,我收到此错误:
Cannot invoke an expression whose type lacks a call signature. Type '{ <U>(onFulfill?: (value: any) => U | PromiseLike<U>, onReject?: (error: any) => U | PromiseLike<...' has no compatible call signatures.
我很困惑,因为Promise.reject
有一个then
方法。这是代码:
const funcThatReturnsPromise = (data) => {
const validatedData = validateKeyData(data)
if(validatedData) {
return request.post({
url: '/apiurl',
json: true,
body: validatedData
})
} else {
return Promise.reject('TESTING rejected')
}
}
const funcThatDoesWork = ((req, res) => {
return funcThatReturnsPromise(req.body)
.then(data => {
if(data) {
const theId = data.id
const fullData = { ...req.body, theId }
}
})
.catch(error => {
console.log('ERROR', error)
res.status(500).send({code: 'unknown', message: error})
})
})