My code ended up with a promise that raised some questions. Is it acceptable to resolve one condition with the token string value (resolve(token))
, while resolving another condition with a promise of type Promise<string>
: resolve(resultPromise);
const finalPromise = new Promise<string>(resolve => {
resultPromise.then(token => {
if (Pending) {
resolve(token);
} else if (!this.tokenIsValid(token)) {
resultPromise = requestToken();
resolve(resultPromise);
} else {
resolve(token);
}
});
I attempted to change it like this and encountered a TypeScript error:
//throwing error error noUnnecessaryCallbackWrapper: No need to wrap 'resolve' in another function. Just use it directly.
#second version
const finalPromise = new Promise<string>(resolve => {
resultPromise.then(token => {
if (Pending) {
resolve(token);
} else if (!this.tokenIsValid(token)) {
requestToken().then(token => resolve(token));
} else {
resolve(token);
}
});
If I return resolve(resultPromise)
, what would be the type of finalPromise? My concern lies in another function receiving finalPromise
where the input type is Promise<string>
. How can I ensure that finalPromise returns a Promise of type string without causing any confusion? Your guidance is greatly appreciated.