Javascript承诺调用Promise混淆

时间:2016-11-08 07:41:03

标签: javascript typescript

我正在使用Javascript / Typescript创建一个Promise,它返回格式化的PersonModel对象。但是我遇到了构建错误:

PersonService.ts

private encryptPerson(person: PersonModel): Promise<PersonModel> {
    return new Promise<PersonModel>(resolve => {  // <== line 332
        let password: string = person.password;
        this.encrypt(password).then((ciphertext: string) => {
            person.password = ciphertext;
            resolve(person);
        });
    },
        error => {
            console.error(error)
        });
}

private encrypt(value: string): Promise<string> {
    return new Promise<string>(resolve => {
        this.encrypter.encrypt(value).then((result) => {
            resolve(result);
        },
            error => {
                console.error(error)
            });
    });
}

错误

ERROR in ./app/pages/service/personService.ts
(332,16): error TS2346: Supplied parameters do not match any signature of call target.

我应该如何构建这种方式的任何帮助。

更新

来自T.J.的建议下面的克劳德,我有以下内容:

private encryptPerson(person: PersonModel): Promise<PersonModel> {
    return new Promise<PersonModel>(resolve => {
        let password: string = person.password;
        this.encrypt(password).then((ciphertext: string) => {
            person.password = ciphertext;
            resolve(person);
        });
    });
}

private encrypt(value: string): Promise<string> {
    return new Promise<string>(resolve => {
        this.encrypter.encrypt(value).then((result: string) => {
            resolve(result);
        });
    });
}

1 个答案:

答案 0 :(得分:2)

您使用两个参数调用Promise构造函数:

private encryptPerson(person: PersonModel): Promise<PersonModel> {
    return new Promise<PersonModel>(resolve => { // <== line 332
            let password: string = person.password;
            this.encrypt(password).then((ciphertext: string) => {
                person.password = ciphertext;
                resolve(person);
            });
        },
        error => {                  // ** This is the
            console.error(error)    // ** second argument
        });                         // ** to the constructor
}

只需要一个参数。我怀疑第二个箭头函数是要附加到第一个箭头函数中的then

另外,如果我对第二个函数的意思是正确的,请注意它会使用undefined作为分辨率值将拒绝转换为分辨率。由于undefined不是PersonModel,我猜测这也会成为一个问题。

相关问题