Javascript 所有([anotherPromise,anotherPromise])当嵌套承诺父级未阻止时
在下面的示例中,Javascript 所有([anotherPromise,anotherPromise])当嵌套承诺父级未阻止时,javascript,node.js,typescript,Javascript,Node.js,Typescript,在下面的示例中,get\u bc\u templates()在get\u bc\u template()之前返回 我需要帮助重写此文件,以便get\u bc\u templates返回一个数组get\u bc\u template()=>collective\u product(get\u bc\u template()一次只运行一个非常好) 承诺。所有都需要一个数组作为参数,您要传递的是数组的数组: [mfpns.map(async item=>等待这个。获取bc_模板(item,cnetDB
get\u bc\u templates()
在get\u bc\u template()
之前返回
我需要帮助重写此文件,以便
get\u bc\u templates
返回一个数组get\u bc\u template()=>collective\u product
(get\u bc\u template()一次只运行一个非常好) 承诺。所有
都需要一个数组作为参数,您要传递的是数组的数组:
[mfpns.map(async item=>等待这个。获取bc_模板(item,cnetDB))]
map函数已经返回了一个数组。因此,您拥有的是一个数组中的承诺数组:Promise.all([[Promise,anotherPromise,…]])
因此,Promise.all
只会尝试等待数组,而不会等待里面的承诺
应移除映射
函数周围的数组括号:
const templates=wait Promise.all(mfpns.map(异步项=>wait this.get_bc_模板(项,cnetDB));
您没有将承诺数组传递给承诺。所有,您传递的是一个数组数组(内部数组由映射构建,外部数组由[]
文本构建)。你应该做什么
async get_bc_templates(mfpns, cnetDB) {
const templates = await Promise.all(mfpns.map(item => this.get_bc_template(item, cnetDB)));
console.log(`prints immediately. before get_bc_template`.green.bold, templates)
return templates.map(template => template.bigCommerce_object);
}
非常感谢。跟进==>console.log inget\u bc\u模板
在正确的时间打印正确的数据。。。但是父函数具有未定义的foreach。。。有什么想法吗?@Omar我认为这可能与您试图访问模板.bigCommerce\u object
这不是您在get\u bc\u template()
中返回的对象上定义的键有关。模板上唯一的键是CNET_data
、JAX_data
和suggestived_retail
。请原谅我的迂腐,但当它是一个只能包含一个数组的数组时,它能被称为数组数组吗P@Klaycon迂腐的回答:是的,我们可以,如果我们指的是类型而不是值。即使是[]
也可能是一个数字数组,尽管它不包含一个-这与[[]
或[[[]]
不同,后者是一个承诺数组。公平地说,但在像javascript这样的语言中,类型可能会有所不同,我认为准确地引用它所包含的内容是有意义的。
async get_bc_templates(mfpns, cnetDB) {
const templates = await Promise.all(mfpns.map(item => this.get_bc_template(item, cnetDB)));
console.log(`prints immediately. before get_bc_template`.green.bold, templates)
return templates.map(template => template.bigCommerce_object);
}