以下代码:
function* logGenerator() {
console.log(0);
console.log(1, yield);
console.log(2, yield);
console.log(3, yield);
}
var gen = logGenerator();
// the first call of next executes from the start of the function
// until the first yield statement
gen.next(); // 0
gen.next('pretzel'); // 1 pretzel
gen.next('california'); // 2 california
gen.next('mayonnaise'); // 3 mayonnaise
我在下面的"将参数传递到生成器"部分中从以下站点获取了这段代码。我正在尝试在MarkLogicQconsole中生成此代码Javascipt站点
我得到的前三个项目的输出是
"done": false
"done": false
"done": false
"done": true
//instead of the expected output
gen.next(); // 0
gen.next('pretzel'); // 1 pretzel
gen.next('california'); // 2 california
gen.next('mayonnaise'); // 3 mayonnaise
有人知道为什么会有这种行为吗?或者我必须做什么不同的事情吗?
Why
:
您使用的console.log
是将文本输出到控制台的JavaScriptWebneneneba API。MarkLogicconsole.log
将文本输出到server log file
。如果您检查MarkLogic日志文件{MarkLogic-root-directory}/{port-number}_ErrorLog
,您应该看到:
{timestamp} Info: 0
{timestamp} Info: 1 pretzel
{timestamp} Info: 2 california
{timestamp} Info: 3 mayonnaise
要将结果输出到MarkLogic查询控制台,请串行化然后迭代参数:
function* logGenerator(arr) {
for (var chain of arr) {
yield chain;
}
}
let arrObj = ['0', '1 pretzel', '2 california', '3 mayonnaise']
const result = []
for(const chain of logGenerator(arrObj)) {
result.push(chain);
}
result;