(我对node、es6和typescript相当陌生。这是我实际正在开发的第一个应用程序)我有以下代码,我试图在typescript中引用这些代码来连接到dynamodb,但我无法理解语法的通用部分:
我试着研究了typescript和es6语法,但没有任何有用的东西。
public readonly getItem = async (
tableName: string,
key: AttributeMap
): Promise<AttributeMap | null> =>
this.dynamodbGet({
TableName: tableName,
Key: key
}).then(({ Item }) => (Item !== undefined ? Item : null));我不能理解这部分代码的语法
: Promise<AttributeMap | null> =>发布于 2019-02-02 17:40:53
发布于 2019-02-02 17:38:38
这是类的一部分。它定义了类实例上的箭头方法。它使用与this proposal不同的TypeScript实现的类字段。与JavaScript的其余语法差异是类型。Promise<AttributeMap | null>表示一个函数返回AttributeMap | null类型的promise;所有async函数都是按照设计返回promise的。
TypeScript将编译成的ES6副本将如下所示:
constructor() {
this.getItem = (tableName, key) => {
return this.dynamodbGet({ TableName: tableName, Key: key })
.then(({ Item }) => (Item !== undefined ? Item : null));
};
}这里使用async是不合理的,因为该函数使用原始的promises,并且不能从await语法中获益。
https://stackoverflow.com/questions/54491724
复制相似问题