[英]TypeScript Type 'Promise<void | Object>' is not assignable to type 'Promise<Object>' on Promise.then()
I am currently trying to implement a service for caching document in base64 format.我目前正在尝试以 base64 格式实现缓存文档的服务。 I want this service to fetch a document from sessionStorage, and if there is no document in sessionStorage, fetch it from IRequestService and then save it to sessionStorage.我希望这个服务从 sessionStorage 中获取文档,如果 sessionStorage 中没有文档,则从 IRequestService 中获取它,然后将其保存到 sessionStorage。 I tried this approach, but I am getting a type error我尝试了这种方法,但出现类型错误
Type 'Promise<void | GetDocumentResult>' is not assignable to type 'Promise<GetDocumentResult>'.
The code looks like this:代码如下所示:
getDocument(requestId: string, documentId: DocumentID, requestService: IRequestService): Promise<GetDocumentResult> {
if (this.storageKey in sessionStorage) {
const documentsMap: Map<DocumentID, GetDocumentResult> = new Map(JSON.parse(sessionStorage.getItem(this.storageKey)!))
if (documentsMap.get(documentId) !== undefined) {
return new Promise(resolve => resolve(documentsMap.get(documentId)!))
}
}
return requestService.getDocument(requestId, documentId)
.then(value => {this.setDocument(documentId, value)})
}
I was expecting return type of Promise< GetDocumentResult >.then to be Promise< GetDocumentResult >, but for some reason which I dont understand, it's Promise<void |我期待 Promise<GetDocumentResult>.then 的返回类型是 Promise<GetDocumentResult>,但由于某种我不明白的原因,它是 Promise<void | GetDocumentResult>获取文档结果>
Do someone knows why this is happening?有人知道为什么会这样吗?
Thank you谢谢
Most likely you have an issue because you are not returning anything inside your .then()很可能您遇到了问题,因为您没有在.then()中返回任何内容
here:这里:
.then(value => {this.setDocument(documentId, value)})
and that makes your returned type be Promise<void>
instead of Promise<GetDocumentResult>
这使您返回的类型是Promise<void>
而不是Promise<GetDocumentResult>
final versions should be like here:最终版本应该像这里:
getDocument(requestId: string, documentId: DocumentID, requestService: IRequestService): Promise<GetDocumentResult> {
if (this.storageKey in sessionStorage) {
const documentsMap: Map<DocumentID, GetDocumentResult> = new Map(JSON.parse(sessionStorage.getItem(this.storageKey)!))
if (documentsMap.get(documentId) !== undefined) {
return new Promise(resolve => resolve(documentsMap.get(documentId)!))
}
}
return requestService.getDocument(requestId, documentId)
.then(value => { return this.setDocument(documentId, value)})
}
As already explained, your then
callback does return void
not the result.如前所述,您的then
回调确实返回void
而不是结果。 You're looking for您正在寻找
getDocument(requestId: string, documentId: DocumentID, requestService: IRequestService): Promise<GetDocumentResult> {
if (this.storageKey in sessionStorage) {
const documentsMap: Map<DocumentID, GetDocumentResult> = new Map(JSON.parse(sessionStorage.getItem(this.storageKey)!));
const document = documentsMap.get(documentId);
if (document !== undefined) {
return Promise.resolve(document);
}
}
return requestService.getDocument(requestId, documentId).then(value => {
this.setDocument(documentId, value);
return value;
// ^^^^^^^^^^^^
});
}
Btw, I would recommend to extract the cache lookup and storage ( setDocument
?) into a separate service, and to have that service keep the documentsMap
in memory instead of parsing JSON every time getDocument
is called.顺便说一句,我建议将缓存查找和存储( setDocument
?)提取到单独的服务中,并让该服务将documentsMap
保存在 memory 中,而不是每次getDocument
时都解析 JSON 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.