Javascript 如何将新映射与async/await一起使用?
我有i18n服务的课程。我想在新地图中使用它。现在^I出现错误“没有重载匹配此调用”Javascript 如何将新映射与async/await一起使用?,javascript,nestjs,Javascript,Nestjs,我有i18n服务的课程。我想在新地图中使用它。现在^I出现错误“没有重载匹配此调用” public async getContent() { const a = new Map([ [ 'AA', new Map([ 'desc', [ await this.i18n.t('Hello') ] ] ])
public async getContent() {
const a = new Map([
[
'AA',
new Map([
'desc',
[
await this.i18n.t('Hello')
]
]
])
]
]);
附言:
当第二个映射仅返回一对时工作
const a = new Map([
[
'AA',
new Map([
['desc', [await this.i18n.t('Hello'), await this.i18n.t('Hello')]],
])
]
]);
但是^当我添加新的一对时,它不起作用
new Map([
['111', 222]
['desc', [await this.i18n.t('Hello'), await this.i18n.t('Hello')]],
])
实际上这应该是可能的,因为您的语法不正确(缺少
[…]
)。考虑这个简单的例子:
function fakeI18n(someStr) {
return new Promise((resolve => {
setTimeout(() => {
resolve('translatedString')
}, 200);
}))
}
(async () => {
const result = new Map([
[
'AA',
new Map([
['111', 222],
[
'desc',
[await fakeI18n('hello'), await fakeI18n('hello')]
]])
]
]);
console.log(result)
})()
这将打印:
Map(1) {
'AA' => Map(2) {
'111' => 222,
'desc' => [ 'translatedString', 'translatedString' ]
}
}
实际上这应该是可能的,因为您的语法不正确(缺少
[…]
)。考虑这个简单的例子:
function fakeI18n(someStr) {
return new Promise((resolve => {
setTimeout(() => {
resolve('translatedString')
}, 200);
}))
}
(async () => {
const result = new Map([
[
'AA',
new Map([
['111', 222],
[
'desc',
[await fakeI18n('hello'), await fakeI18n('hello')]
]])
]
]);
console.log(result)
})()
这将打印:
Map(1) {
'AA' => Map(2) {
'111' => 222,
'desc' => [ 'translatedString', 'translatedString' ]
}
}
为什么不在完成之前等待结果并将其设置为Map?我不确定地图是否可以这样使用。我有一个大的地图数组。为什么不先等待结果,完成后再将其设置为地图?我不确定地图是否可以用这种方式使用。我有一个大地图数组。谢谢,没有看到用这种方式使用的
Map
,今天会学到一些新东西:)它可以工作,但我的第二个地图返回数组添加了一个问题,您需要在[…]中指定每一对。查看我的更新。@VladimirGolub:有任何反馈吗?谢谢,没有看到以这种方式使用的Map
,今天将学到一些新东西:)它可以工作,但我的第二个Map return数组添加了一个问题,您需要在[…]中指定每一对。查看我的更新。@VladimirGolub:有什么反馈吗?