JS:我可以在将一个过滤后的数组推送到另一个数组后使用 'then' 吗?



我有一个称为 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()都不会有任何区别

相关内容

最新更新