Javascript 运行 行代码是否有序?并完成处理然后继续?
Does Javascript run lines of code in order? and finish processing then move on?
我第一次尝试在 Javascript 中编写一些代码,我猜我在概念上没有得到一些东西!
以下代码对我来说工作正常:
var db = new alasql.Database("db");
db.exec('CREATE TABLE IF NOT EXISTS Myonetwo;');
var aaa = db.exec('select * into Myonetwo from json("http://localhost:8080/app1")',[],function(res){
db.exec('select * from Myonetwo;',[],function(bbb){
console.log(bbb.length);
});
});
但是这个功能相同但没有内嵌函数的,不行。
var db = new alasql.Database("db");
db.exec('CREATE TABLE IF NOT EXISTS Myonetwo;');
var aaa = db.exec('select * into Myonetwo from json("http://localhost:8080/app1")');
var bbb = db.exec('select * from Myonetwo;');
console.log(bbb.length);
此外,是否将结果作为函数定义为所有 Javascript 的参数之一?
这是因为 exec
函数是 异步的 ,因此在您的第二个示例中, bbb
赋值行将先于 aaa
赋值行整理。
这就是为什么 exec
有一个 回调函数 作为最后一个参数。
在您的第一个代码片段中,事件的时间轴类似于:
- 第一次执行调用
- 一段时间过去了...
- 第一次执行回调发生
- 第二个 exec 调用执行
- 第二次执行回调发生
在这种情况下,您知道第二次 exec 调用将在第一次 exec 调用之后发生。
您的第二个代码片段的时间轴将是:
- 第一次执行调用
- 第二次执行调用
- 第一个或第二个 exec 调用完成(不确定)
- 第一个或第二个 exec 调用完成(不确定)
你可以看到这对你的逻辑有影响。
这里有一篇很好的文章,可以了解更多关于异步编程的信息:https://blog.risingstack.com/asynchronous-javascript/
是 Javascript 按顺序执行 运行 行代码。但也包含 asynchronous/deferred 编程元素。
概念可以在这里查https://developer.mozilla.org/en/docs/Web/JavaScript/EventLoop
归结为:
console.log("a");
console.log("b"); // output will be a, b, c
console.log("c");
// output will be a, b, c
然后再
console.log("a");
setTimeout(function(){
concole.log("b");
},0);
console.log("c");
//output will be a, c, b
原因是打印"b"的代码两次都出现在console.log("c")
之前,但确实不是两次都执行过。在第二种情况下,它通过 window.setTimeout(function, timeout)
函数
在 EventLoop 中排队
正如其他人所说,这是因为 javascript,或者在这种情况下,NodeJS 本质上是异步的。原因是因为 delegate I/O 任务非常有效,因为它们的执行时间比普通任务长得多。
我觉得可以打个比方:
JavaScript 就像一个建造者。让我们称他(或她)为 Bob.
场景 1 - 等同于所讨论的第二个示例
Customer asks Bob to build a house.
Bob is too busy to do the work alone so he asks Joe, a
brick layer to help.
Joe starts laying bricks.
In the meantime, Bob asks Paul the plumber to put pipes in the walls.
Pauls beings to lay pipes ... hang on .. there are no
walls yet
[malfunction]
在 JavaScript 中,代码可能如下所示:
Joe.layBricks()
Paul.layPipes()
这基本上就是你的第二个例子中发生的事情
场景 2 - 等同于您的第一个示例
鲍勃再次尝试:
Customer asks Bob to build a house.
Bob asks Joe to lay some bricks.
But this time he tells Joe that when he is finished he must tell
Paul (known as a callback) he is ready.
Paul already knows what he needs to do but just needs to know when he
can do it.
Much better. The work will get done.
Now Bob can even ask painter Pat to paint the house when the plumbing
is done in the same way.
代码如下所示:
Joe.layBricks(Paul.layPipes(Pat.paintWalls()))
看着眼熟? (应该接近示例 1)
其他语言是如何实现的
对于 Bob 来说,一件容易的事情就是自己完成所有工作,但这不会非常有效或可扩展。这就是许多语言的工作方式。但是 NodeJS 有一个更好的方法,这就是回调发挥作用的地方。
其他高级语言允许您定义自己的线程。但它们非常复杂,难以理解和维护。
您可以重写代码:
var db = new alasql.Database("db");
var res = db.exec('CREATE TABLE IF NOT EXISTS Myonetwo;\
select * into Myonetwo from json("http://localhost:8080/app1");\
select * from Myonetwo;');
console.log(res);
在这种情况下,AlaSQL 将按顺序 运行 所有运算符(即使它们是异步的)并将它们保存到 res 变量中。
在其他情况下,您需要使用回调重写代码:
var db = new alasql.Database("db");
var res = db.exec('CREATE TABLE IF NOT EXISTS Myonetwo;');
var res = db.exec('select * into Myonetwo from \
json("http://localhost:8080/app1")',[],function(){
var bbb = db.exec('select * from Myonetwo;');
console.log(res);
});
在您的示例中,您只需要对第二行使用回调,因为 AlaSQL 中的 JSON() 函数是异步的。
我第一次尝试在 Javascript 中编写一些代码,我猜我在概念上没有得到一些东西!
以下代码对我来说工作正常:
var db = new alasql.Database("db");
db.exec('CREATE TABLE IF NOT EXISTS Myonetwo;');
var aaa = db.exec('select * into Myonetwo from json("http://localhost:8080/app1")',[],function(res){
db.exec('select * from Myonetwo;',[],function(bbb){
console.log(bbb.length);
});
});
但是这个功能相同但没有内嵌函数的,不行。
var db = new alasql.Database("db");
db.exec('CREATE TABLE IF NOT EXISTS Myonetwo;');
var aaa = db.exec('select * into Myonetwo from json("http://localhost:8080/app1")');
var bbb = db.exec('select * from Myonetwo;');
console.log(bbb.length);
此外,是否将结果作为函数定义为所有 Javascript 的参数之一?
这是因为 exec
函数是 异步的 ,因此在您的第二个示例中, bbb
赋值行将先于 aaa
赋值行整理。
这就是为什么 exec
有一个 回调函数 作为最后一个参数。
在您的第一个代码片段中,事件的时间轴类似于:
- 第一次执行调用
- 一段时间过去了...
- 第一次执行回调发生
- 第二个 exec 调用执行
- 第二次执行回调发生
在这种情况下,您知道第二次 exec 调用将在第一次 exec 调用之后发生。
您的第二个代码片段的时间轴将是:
- 第一次执行调用
- 第二次执行调用
- 第一个或第二个 exec 调用完成(不确定)
- 第一个或第二个 exec 调用完成(不确定)
你可以看到这对你的逻辑有影响。
这里有一篇很好的文章,可以了解更多关于异步编程的信息:https://blog.risingstack.com/asynchronous-javascript/
是 Javascript 按顺序执行 运行 行代码。但也包含 asynchronous/deferred 编程元素。
概念可以在这里查https://developer.mozilla.org/en/docs/Web/JavaScript/EventLoop
归结为:
console.log("a");
console.log("b"); // output will be a, b, c
console.log("c");
// output will be a, b, c
然后再
console.log("a");
setTimeout(function(){
concole.log("b");
},0);
console.log("c");
//output will be a, c, b
原因是打印"b"的代码两次都出现在console.log("c")
之前,但确实不是两次都执行过。在第二种情况下,它通过 window.setTimeout(function, timeout)
函数
正如其他人所说,这是因为 javascript,或者在这种情况下,NodeJS 本质上是异步的。原因是因为 delegate I/O 任务非常有效,因为它们的执行时间比普通任务长得多。
我觉得可以打个比方:
JavaScript 就像一个建造者。让我们称他(或她)为 Bob.
场景 1 - 等同于所讨论的第二个示例
Customer asks Bob to build a house.
Bob is too busy to do the work alone so he asks Joe, a brick layer to help.
Joe starts laying bricks.
In the meantime, Bob asks Paul the plumber to put pipes in the walls.
Pauls beings to lay pipes ... hang on .. there are no walls yet
[malfunction]
在 JavaScript 中,代码可能如下所示:
Joe.layBricks()
Paul.layPipes()
这基本上就是你的第二个例子中发生的事情
场景 2 - 等同于您的第一个示例
鲍勃再次尝试:
Customer asks Bob to build a house.
Bob asks Joe to lay some bricks.
But this time he tells Joe that when he is finished he must tell Paul (known as a callback) he is ready.
Paul already knows what he needs to do but just needs to know when he can do it.
Much better. The work will get done.
Now Bob can even ask painter Pat to paint the house when the plumbing is done in the same way.
代码如下所示:
Joe.layBricks(Paul.layPipes(Pat.paintWalls()))
看着眼熟? (应该接近示例 1)
其他语言是如何实现的
对于 Bob 来说,一件容易的事情就是自己完成所有工作,但这不会非常有效或可扩展。这就是许多语言的工作方式。但是 NodeJS 有一个更好的方法,这就是回调发挥作用的地方。
其他高级语言允许您定义自己的线程。但它们非常复杂,难以理解和维护。
您可以重写代码:
var db = new alasql.Database("db");
var res = db.exec('CREATE TABLE IF NOT EXISTS Myonetwo;\
select * into Myonetwo from json("http://localhost:8080/app1");\
select * from Myonetwo;');
console.log(res);
在这种情况下,AlaSQL 将按顺序 运行 所有运算符(即使它们是异步的)并将它们保存到 res 变量中。
在其他情况下,您需要使用回调重写代码:
var db = new alasql.Database("db");
var res = db.exec('CREATE TABLE IF NOT EXISTS Myonetwo;');
var res = db.exec('select * into Myonetwo from \
json("http://localhost:8080/app1")',[],function(){
var bbb = db.exec('select * from Myonetwo;');
console.log(res);
});
在您的示例中,您只需要对第二行使用回调,因为 AlaSQL 中的 JSON() 函数是异步的。