typescript - Javascript Promise.all confusion -
i using javascript/typescript in order format password
value on collection of personmodel
objects. executing promise
on each element in collection.
i using promise.all
allow promises complete, , want return formatted collection.
however, error @ build time.
personservice.ts
private decryptpersons(persons: personmodel[]): promise<personmodel[]> { return new promise<personmodel[]>(resolve => { let promises: array<promise<string>> = []; let decryptedpersons: personmodel[] = []; (let i: number = 0; < persons.length; i++) { let ciphertext: string = persons[i].password; promises.push(this.decrypt(ciphertext).then((password: string) => { // <== line 357 persons[i].password = password; decryptedpersons.push(persons[i]); })); } promise.all(promises).then(() => { resolve(decryptedpersons); }); }); } private decrypt(value: string): promise<string> { return new promise<string>(resolve => { this.encrypter.decrypt(value).then((result: string) => { resolve(result); }); }); }
error
error in ./app/pages/service/personservice.ts (357,31): error ts2345: argument of type 'promise<void>' not assignable parameter of type 'promise<string>'. type 'void' not assignable type 'string'.
i no expert javascript, structure may me incorrect. if can advise, appreciate it.
you're trying push
following promises
array:
this.decrypt(ciphertext).then((password: string) => { // <== line 357 persons[i].password = password; decryptedpersons.push(persons[i]); })
but function here doesn't return anything, going evaluate promise<void>
.
you're abusing "explicit promise construction antipattern" in several places here.
give try:
private decryptpersons(persons: personmodel[]): promise<personmodel[]> { return promise.all(persons.map(person => this.decrypt(person.password) .then(password => { person.password = password; return person; }); )); } private decrypt(value: string): promise<string> { return this.encrypter.decrypt(value); }
Comments
Post a Comment