function placeOrder(orderNo) {
console.log("Order is: " + orderNo);
setTimeout(deliver(orderNo),5000);
}
function deliver(orderNo) {
console.log("Item is delivered with Order No.- " + orderNo);
}
placeOrder(1);
placeOrder(2);
placeOrder(3);
placeOrder(4);
placeOrder(5);
placeOrder(6);
上面的代码是一个nodejs代码,它的输出应该是:
order No:1
order No:2
order No:3
order No:4
order No:5
order No:6
Item is delivered with order No.- 1
Item is delivered with order No.- 2
Item is delivered with order No.- 3
Item is delivered with order No.- 4
Item is delivered with order No.- 5
Item is delivered with order No.- 6
但我得到这样的输出:
order No:1
Item is delivered with order No.- 1
order No:2
Item is delivered with order No.- 2
order No:3
Item is delivered with order No.- 3
order No:4
Item is delivered with order No.- 4
order No:5
Item is delivered with order No.- 5
order No:6
Item is delivered with order No.- 6
我在单线程和异步回调的概念中的某个地方出错了。请有人解释一下代码是如何工作的。