节点代码不阻塞?



>我有一个构造函数,它使用承诺的dynogels从DynamoDB获取数据,以填充对象的部分属性。 因此,在实例化该对象的实例后,不会填充该属性,下面是代码的摘录:

export class QueryAuthoriser {
authPerms: [AuthPerms];
constructor (data: string) {
AuthPermsDDB.scan().execAsync().then ( (perms) => {
perms.Items.forEach(element => {
this.authPerms[element.name] = <AuthPerms> element.attrs
})
}).catch (err => {
console.log ('%%%%%%%%%%%%%% Err loading authPerms: ', err)
})
}
authFieldAccess (fieldName: string, args?:any): Promise<boolean> {
return new Promise ((resolve, reject) => {
console.log ('________________ authFieldAccess called for: ', fieldName)
console.log ('________________ this.authPerms entry: ', this.authPerms[fieldName])
resolve (true)
})
[...]
}

因此,当调用authFieldAccess方法时,字段this.authPerms是未定义的。我该如何解决这个问题?

谢谢,我正在以艰难的方式学习节点和打字稿:O

您通常不希望在构造函数中执行异步操作,因为它会使创建对象变得复杂,然后知道异步操作何时完成或是否有错误,因为您需要允许构造函数返回对象,而不是告诉您异步操作何时完成的承诺。

有几种可能的设计选项:

选项 #1:不要在构造函数中执行任何异步操作。然后,添加一个具有适当名称的新方法,该方法执行异步操作并返回 promise。

在您的情况下,您可以使新方法scan()返回承诺。 然后,您将通过创建对象,然后调用 scan 然后使用返回的承诺来了解数据何时有效来使用对象。

我自己不懂 TypeScript,所以我会给出你的代码的修改版本,但无论是 TypeScript 还是纯 Javascript,概念都是一样的:

export class QueryAuthoriser {
authPerms: [AuthPerms];
constructor (data: string) {
}
scan () {
return AuthPermsDDB.scan().execAsync().then ( (perms) => {
perms.Items.forEach(element => {
this.authPerms[element.name] = <AuthPerms> element.attrs
})
}).catch (err => {
console.log ('%%%%%%%%%%%%%% Err loading authPerms: ', err)
})
}
}
// usage
let obj = new QueryAuthoriser(...);
obj.scan(...).then(() => {
// the object is full initialized now and can be used here
}).catch(err => {
// error here
})

选项 #2:在构造函数中启动异步操作,并在实例数据中使用 promise,以便调用方知道何时完成所有操作。

export class QueryAuthoriser {
authPerms: [AuthPerms];
constructor (data: string) {
this.initialScan = AuthPermsDDB.scan().execAsync().then ( (perms) => {
perms.Items.forEach(element => {
this.authPerms[element.name] = <AuthPerms> element.attrs
})
}).catch (err => {
console.log ('%%%%%%%%%%%%%% Err loading authPerms: ', err)
})
}
}
// usage
let obj = new QueryAuthoriser(...);
obj.initialScan.then(() => {
// the object is full initialized now and can be used here
}).catch(err => {
// error here
});

选项 #3:使用返回解析为对象本身的承诺的工厂函数。

export createQueryAuthorizer;
function createQueryAuthorizer(...) {
let obj = new QueryAuthorizer(...);
return obj._scan(...).then(() => {
// resolve with the object itself
return obj;
})
}
class QueryAuthoriser {
authPerms: [AuthPerms];
constructor (data: string) {
}
_scan () {
return AuthPermsDDB.scan().execAsync().then ( (perms) => {
perms.Items.forEach(element => {
this.authPerms[element.name] = <AuthPerms> element.attrs
})
}).catch (err => {
console.log ('%%%%%%%%%%%%%% Err loading authPerms: ', err)
})
}
}
// usage
createQueryAuthorizer(...).then(obj => {
// the object is fully initialized now and can be used here
}).catch(err => {
// error here
});

出于多种原因,我更喜欢选项 #3。 它在工厂函数中捕获一些共享代码,每个调用方都必须在其他方案中执行这些代码。 它还会阻止对对象的访问,直到正确初始化对象。 另外两种方案只需要文档和编程纪律,很容易被滥用。

最新更新