Cheerio的每个循环都不会提前爆发

在Cheerio / Jquery 文档中指出,返回false应该尽早打破每个循环。

我有以下代码:

"use strict"; let cheerio = require('cheerio'); let $ = cheerio.load('<a href="www.test.com"></a><a href="www.test.com"></a>'); $('a').each(function(index,value){ console.log(index); return false; }); 

它应该在我的脑海中打印0到控制台,但它打印0和1.我错过了什么?

代码很好。 如果你看看cheerio的源代码,你可以看到,如果你返回false,那么循环就不会继续。

https://github.com/cheeriojs/cheerio/blob/master/lib/api/traversing.js#L298

 exports.each = function(fn) { var i = 0, len = this.length; while (i < len && fn.call(this[i], i, this[i]) !== false) ++i; return this; };