node 14.7.0, npm 6.14.7, typescript 3.7.3.
I have a method that queries a postgres database and, depending on a param, will return the first row it finds or the entire result from the query. Similar to this:
async getRecord(options: {singleRow: boolean}): Promise<QueryResult> | Promise<Record> {
const result:QueryResult = await postgres.query(...);
if (options && options.singlerow) {
return result.rows[0];
}
return result;
}
However, I receive the following error: The return type of an async function or method must be the global Promise<T> type.
I do not receive an error when I remove the | Promise<Record>
| Promise<Record>
section, even though the first return (in the if statement) returns a Promise<Record>
. What should I do to fix this?
Return Promise<QueryResult | Record>
Promise<QueryResult | Record>
async
functions need a Promise to box whatever type they return, there's just one top level Promise-type-generic that wraps the return type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.