如何在javascript中传递一个完整的对象到promise async await



我只是想排序这个没有运气如何理解这个async await承诺

此方法将用于在更新操作之前调用对话框材料。对话框有一个订阅,这就是我在这里使用async await的原因。

这是代码:

async execute(task: ITask): Promise<boolean> {
return new Promise((resolve) => {
this.confirmation
.confirmDelete(`${task.id} - ${task.title}`)
.subscribe(async (confirmed) => {
if (confirmed) {
await this.repository.update(**<Task[]**>(task.id));
this.dialogService
.openConfirmDialog('Are you really want to delete/update/create?')
.afterClosed();
}
resolve(confirmed);
});
});
}
}

如何在<Task中传递对象[]>

感谢你的帮助

感谢编辑:

这是task.repository.ts

import { environment } from './../../../environments/environment';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ITask } from '../models/itask';
@Injectable({
providedIn: 'root',
})
export class TaskRepository {
constructor(private httpClient: HttpClient) {}
create(task: ITask): Promise<ITask> {
return this.httpClient
.post<ITask>(`${environment.api}/tasks`, task)
.toPromise();
}
update(entity: ITask): Promise<ITask> {
const { id, ...data } = entity;
return this.httpClient
.put<ITask>(`${environment.api}/tasks/${id}`, data)
.toPromise();
}
getById(id: string): Promise<ITask> {
return this.httpClient
.get<ITask>(`${environment.api}/tasks/${id}`)
.toPromise();
}
getAll(): Promise<ITask[]> {
return this.httpClient
.get<ITask[]>(`${environment.api}/tasks/`)
.toPromise();
}
async delete(id: string): Promise<void> {
await this.httpClient.delete(`${environment.api}/tasks/${id}`).toPromise();
return;
}
}

更新2

使用task作为参数,解决了这个问题,但是在我的。ts组件开始抱怨

import { ITask } from './../../models/itask';
import { GetTaskHandler } from './../../business-rules/get-task.handler';
import { UpdateTaskHandler } from './../../business-rules/update-task.handler';
import { CreateTaskHandler } from './../../business-rules/create-task.handler';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';

@Component({
selector: 'app-task-form-page',
templateUrl: './task-form-page.component.html',
styleUrls: ['./task-form-page.component.scss'],
})
export class TaskFormPageComponent implements OnInit {
pageTitle = 'Nova tarefa';
// configuração do formulário
form = this.formBuild.group({
title: [''],
description: [''],
done: [false],
});
get title(): FormControl {
return this.form.get('title') as FormControl;
}
get description(): FormControl {
return this.form.get('description') as FormControl;
}
get done(): FormControl {
return this.form.get('done') as FormControl;
}
taskId: string | undefined = undefined;
constructor(
private formBuild: FormBuilder,
private activatedRouter: ActivatedRoute,
private createTaskHandler: CreateTaskHandler,
private updateTaskHandler: UpdateTaskHandler,
private getTaskHandler: GetTaskHandler,
) {}
async ngOnInit(): Promise<void> {
const paramId = this.activatedRouter.snapshot.paramMap.get('id');
if (paramId) {
this.taskId = paramId;
await this.loadTask();
}
}
async loadTask(): Promise<void> {
const response = await this.getTaskHandler.execute(this.taskId || '');
if (response) {
this.pageTitle = 'Editando tarefa';
// atualizando o formulário com os valores retornados pela api
this.form.patchValue({
title: response.title,
description: response.description,
done: response.done,
});
}
}
async onSubmit(): Promise<void> {
const taskToSave: ITask = {
...this.form.value, // pegando todos os valores do formulário
id: this.taskId, // atualizando o id caso exista
};
let response: ITask | undefined;

if (taskToSave.id) {
***response*** = await this.updateTaskHandler.execute(taskToSave);
} else {
response = await this.createTaskHandler.execute(taskToSave);
}
if (response) {
this.taskId = response.id;
}
}
}

我认为你犯了一个错误,更新期望任务对象,所以await this.repository.update(**<Task[]**>(task.id));应该是await this.repository.update(task);我也不认为这是一个好主意,在一个承诺内使用订阅。你能不能试试这样

async execute(task: ITask): Promise<boolean> {

// Convert to promise
const confirmed = await this.confirmation
.confirmDelete(`${task.id} - ${task.title}`).pipe(first()).toPromise();
if (confirmed) {
await this.repository.update(task);
this.dialogService.openConfirmDialog('Are you really want to delete/update/create?')
.afterClosed();
}
return new Promise((resolve) => {
resolve(confirmed);
});
});
}
}

您应该使用await this.repository.update(task);

相关内容

  • 没有找到相关文章

最新更新