我有一个过滤器方法如下:
let newArray = Alerts[symbol].filter(async (alert: IDiscordSymbolAlert, alert_index: number) => {
// If Price when set is greater than alert price, price has to move UP to trigger
if (alert.price_when_set < alert.alert_price) {
hasToGo = Change.UP;
}
// If Price when set is greater than alert price, price has to move DOWN to trigger
else if (alert.price_when_set > alert.alert_price){
hasToGo = Change.DOWN;
}
/**If the hasToGo is UP and current price is greater than alert price, then ALERT USER [OR]
// If the hasToGo is DOWN and current price is lesser than alert price, then ALERT USER **/
if((hasToGo === Change.UP && (price >= alert.alert_price)) ||
(hasToGo === Change.DOWN && (price <= alert.alert_price))) {
/**
* Send Notification that alert has been hit
*/
let userToSend = await discord.users.fetch(alert.userID)
if(!userToSend) return;
//@ts-ignore
userToSend.send(`> @everyonenn${symbol} has reached your alert price - ${alert.alert_price}`);
// remove that alert
guild_modified = true;
return false;
}
return true;
});
// When logged here, it shows the entire array even though certain values have returned false.
// I know they returned false because the block in which they returned false was executed and
//i did receive a notification which was executed in the same block.
console.log(newArray);
在这里记录(console.log)
时,即使某些值返回false,它也会显示整个数组。我知道它们返回false,因为它们返回false的块被执行,并且我确实收到了在同一块中执行的通知。我知道这与async/await
有关,但我不确定是什么。谁来帮我看看。
Alerts[symbol].forEach(async (alert: IDiscordSymbolAlert, alert_index: number) => {
// If Price when set is greater than alert price, price has to move UP to trigger
if (alert.price_when_set < alert.alert_price) {
hasToGo = Change.UP;
}
// If Price when set is greater than alert price, price has to move DOWN to trigger
else if (alert.price_when_set > alert.alert_price){
hasToGo = Change.DOWN;
}
/**If the hasToGo is UP and current price is greater than alert price, then ALERT USER [OR]
// If the hasToGo is DOWN and current price is lesser than alert price, then ALERT USER **/
if((hasToGo === Change.UP && (price >= alert.alert_price)) ||
(hasToGo === Change.DOWN && (price <= alert.alert_price))) {
/**
* Send Notification that alert has been hit
*/
let userToSend = await discord.users.fetch(alert.userID)
if(!userToSend) return;
//@ts-ignore
userToSend.send(`> @everyonenn${symbol} has reached your alert price - ${alert.alert_price}`);
// remove that alert
Alerts![symbol].splice(alert_index, 1);
guild_modified = true;
}
});
console.log(Alerts[symbol]);
因为过滤器不能在这种情况下使用async/await
。这是我使用forEach()
循环并使用splice()
手动删除数组中匹配的对象。但是由于forEach本身是异步的,并且console.log
在异步块之外起作用。它返回的不是新数组,而是未修改的数组。如何返回在forEach块作用域之外使用的新数组?
提前感谢!
不能将async
函数与filter
一起使用。filter
返回回调函数返回真值的所有元素。async
函数总是返回承诺,承诺是对象,所有非null
对象都是真值。
如果你的filter
检查需要async
,你的整个操作是async
,需要这样处理。
这是一个现成的async
过滤函数,它并行地执行元素检查。同样,像所有async
函数一样,它返回结果的承诺:
async function filterAsync(array, callback, thisArg = undefined) {
const flags = await Promise.all(array.map(callback, thisArg));
const result = [];
for (let n = 0; n < array.length; ++n) {
if (flags[n]) {
result.push(array[n]);
}
}
return result;
}
要使用它,你必须消费承诺:
// In an `async` function (assuming you want errors to propagate):
const newArray = await filterAsync(Alerts[symbol], element => /*...keep-or-toss logic...*/);
// ...use `newArray`...
或
// Not in an `async` function
filterAsync(Alerts[symbol], element => /*...keep-or-toss logic...*/)
.then(newArray => {
// ...use `newArray`...
})
.catch(error => {
// ...handle/report error...
});