d3.queue.await 未调用函数
d3.queue.await function not invoked
为什么在这个非常小的示例中没有调用 .await 函数?
HTML
<h1>test</h1>
<div id="a"></div>
<div id="b"></div>
JS
d3.queue()
.defer(a,1)
.await(b,2);
function a(x){
d3.select('#a').text("a is executing with x="+x);
}
function b(err,x){
d3.select('#b').text("b is executing with x="+x);
}
输出
test
a is executing with x=1
它在 docs 的第一句话中:
the callback must be invoked by the task when it finishes.
为了遵守 API 您需要调用回调,它作为最后一个参数提供给您的函数 a()
:
function a(x, callback) {
d3.select('#a').text("a is executing with x="+x);
callback(null, { /* result */ });
}
@altocumulus 用他富有洞察力的评论打败了我,但是因为一个例子说明了一千个字:
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<h1>test</h1>
<div id="a"></div>
<div id="b"></div>
<script>
var q = d3.queue()
.defer(a, 1, 2)
.await(b);
function a(x, y, callback) {
d3.select('#a').text("a is executing with x=" + x);
callback(null, y);
}
function b(err, x) {
d3.select('#b').text("b is executing with x=" + x);
}
</script>
</body>
</html>
为什么在这个非常小的示例中没有调用 .await 函数?
HTML
<h1>test</h1>
<div id="a"></div>
<div id="b"></div>
JS
d3.queue()
.defer(a,1)
.await(b,2);
function a(x){
d3.select('#a').text("a is executing with x="+x);
}
function b(err,x){
d3.select('#b').text("b is executing with x="+x);
}
输出
test
a is executing with x=1
它在 docs 的第一句话中:
the callback must be invoked by the task when it finishes.
为了遵守 API 您需要调用回调,它作为最后一个参数提供给您的函数 a()
:
function a(x, callback) {
d3.select('#a').text("a is executing with x="+x);
callback(null, { /* result */ });
}
@altocumulus 用他富有洞察力的评论打败了我,但是因为一个例子说明了一千个字:
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<h1>test</h1>
<div id="a"></div>
<div id="b"></div>
<script>
var q = d3.queue()
.defer(a, 1, 2)
.await(b);
function a(x, y, callback) {
d3.select('#a').text("a is executing with x=" + x);
callback(null, y);
}
function b(err, x) {
d3.select('#b').text("b is executing with x=" + x);
}
</script>
</body>
</html>