我有一个称为 products
的数组。我还有另一个名为otherProds
的数组。我正在运行一个函数,该功能可以过滤otherProds
,然后使用差异语法将该数组推入products
数组。只有在完成之后,我该怎么做?我尝试使用then
,并且正在记录我的内容,但它也引发了错误TypeError: (intermediate value)(intermediate value).push.apply(...).then is not a function
我的代码:
products.push(...this.filterInventory(otherProds)).then(console.log("do something after"));
"然后"仅适用于承诺。如果" this.filterinventory(其他prods("返回承诺,则可以做到"。
Array.push()
返回已更新的数组计数。因此,push
函数的输出将是数字值,它将没有任何诸如then
的函数,仅在Promise
中可用。此外,push
是同步函数。因此,您可以将console.log
放在下一行中,这将照常工作。
如果您只想将其他产品推到产品上并做任何您想做的事情,就可以像下面一样做。
products.push(...this.filterInventory(otherProds));
console.log("do something");
然后函数返回承诺
no。Array.push()
不返回对象Promise
。而且Array.push()
也不是异步的功能,因此,即使可以,使用then(console.log())
和仅在下一行上只有console.log()
都不会有任何区别