从匿名函数返回将不起作用,即使它不是异步的
Returning from an anonymous function won't work, even though it's not async
我有以下功能:
function filterDesiredURLs(tweet) {
tweet.entities.urls.forEach((url) => {
desiredURLs.forEach((regexPattern) => {
if (regexPattern.test(url['expanded_url'])) {
console.log('hello, im returning');
return true;
}
})
})
}
我这样称呼它:
console.log(filterDesiredURLs(tweet));
其中 tweet 是定义的对象。我可以看到该函数确实是 returning,因为我在控制台中看到了输出 hello, im returning
,但是 console.log(filterDesiredURLs(tweet));
打印了 undefined
。我希望匿名函数作为异步操作的回调传递,但这不是异步的,所以 return 应该可以工作。发生什么事了?
当您这样调用 return
时,您将从最近的函数返回(在本例中,匿名函数作为参数传递给您的内部 forEach
)。
来自docs:
The return statement ends function execution and specifies a value to
be returned to the function caller.
为了实现你的目标,你可以试试这个:
function filterDesiredURLs(tweet) {
let found = false;
tweet.entities.urls.forEach((url) => {
desiredURLs.forEach((regexPattern) => {
if (regexPattern.test(url['expanded_url'])) {
console.log('hello, im returning');
found = true;
/* don't need return because forEach does not expects a function that returns something; and you can't break forEach */
}
})
})
return found;
}
javascript forEach
方法 returns undefined
.
forEach
是一个将数组保持为不可变的操作,return 是一个新数组。在您的代码中,调用了 forEach
方法,但它没有 return 任何内容,因此 undefined
.
return
不跨函数边界运行。它仅来自 innermost 函数 returns。做你想做的事你可能想要 filter
or find
coupled with some
:
function filterDesiredURLs(tweet) {
// First, you were missing a return in the outer function
// Without a return here, *this* function will return `undefined`
return tweet.entities.urls.filter(url => {
// We are using `filter` to reduce the URL list to only
// URLs that pass our test - this inner function needs to return
// a boolean (true to include the URL, false to exclude it)
return desiredURLs.some(regexPattern => {
// Finally, we use `some` to see if any of the regex patterns match
// This method returns a boolean value. If the function passed to it ever
// returns true, it terminates the loop and returns true
// Otherwise, it iterates over the entire array and returns false.
return regexPattern.test(url['expanded_url']);
});
});
}
我有以下功能:
function filterDesiredURLs(tweet) {
tweet.entities.urls.forEach((url) => {
desiredURLs.forEach((regexPattern) => {
if (regexPattern.test(url['expanded_url'])) {
console.log('hello, im returning');
return true;
}
})
})
}
我这样称呼它:
console.log(filterDesiredURLs(tweet));
其中 tweet 是定义的对象。我可以看到该函数确实是 returning,因为我在控制台中看到了输出 hello, im returning
,但是 console.log(filterDesiredURLs(tweet));
打印了 undefined
。我希望匿名函数作为异步操作的回调传递,但这不是异步的,所以 return 应该可以工作。发生什么事了?
当您这样调用 return
时,您将从最近的函数返回(在本例中,匿名函数作为参数传递给您的内部 forEach
)。
来自docs:
The return statement ends function execution and specifies a value to be returned to the function caller.
为了实现你的目标,你可以试试这个:
function filterDesiredURLs(tweet) {
let found = false;
tweet.entities.urls.forEach((url) => {
desiredURLs.forEach((regexPattern) => {
if (regexPattern.test(url['expanded_url'])) {
console.log('hello, im returning');
found = true;
/* don't need return because forEach does not expects a function that returns something; and you can't break forEach */
}
})
})
return found;
}
javascript forEach
方法 returns undefined
.
forEach
是一个将数组保持为不可变的操作,return 是一个新数组。在您的代码中,调用了 forEach
方法,但它没有 return 任何内容,因此 undefined
.
return
不跨函数边界运行。它仅来自 innermost 函数 returns。做你想做的事你可能想要 filter
or find
coupled with some
:
function filterDesiredURLs(tweet) {
// First, you were missing a return in the outer function
// Without a return here, *this* function will return `undefined`
return tweet.entities.urls.filter(url => {
// We are using `filter` to reduce the URL list to only
// URLs that pass our test - this inner function needs to return
// a boolean (true to include the URL, false to exclude it)
return desiredURLs.some(regexPattern => {
// Finally, we use `some` to see if any of the regex patterns match
// This method returns a boolean value. If the function passed to it ever
// returns true, it terminates the loop and returns true
// Otherwise, it iterates over the entire array and returns false.
return regexPattern.test(url['expanded_url']);
});
});
}