[英]Function with return value of other function as variable, async or not?
I have the following: 我有以下内容:
var module = {}
module.setDate = function() {
var d = new Date();
return d;
}
Say I now have: 说我现在有:
function logDate(){
var date = module.setDate();
console.log(date)
console.log('finished')
}
Is the setting of var date
to the return value of module.setDate()
synchronous or asynchronous? 将
var date
设置为module.setDate()
的返回值是同步的还是异步的? Could the console ever look like: 控制台可能看起来像:
undefined
'finished'
I think you're confusing a function call with a constructor. 我认为您将函数调用与构造函数混淆了。
var d = new Date();
This will create a Date
object immediately, that that's what your function is returning. 这将立即创建一个
Date
对象,这就是您的函数返回的内容。
On the other hand, if your function were 另一方面,如果您的职能是
var module = {}
module.setDate = function() {
return function() {
var d = new Date();
return d;
}
}
This would be asynchronous, but not parallelly executed. 这将是异步的,但不会并行执行。 The execution of the function you return would wait until you call the function.
返回的函数的执行将等到调用该函数。
function logDate(){
var date = module.setDate();
console.log(date() /* <--- need these parens, now! */)
console.log('finished')
}
In order to truly execute in parallel, which I think it what you are trying to ask by saying "asynchronous", then I'd suggest reading up on one of these links. 为了真正并行执行(我认为这是您要通过说“异步”来提出的问题),那么建议您阅读这些链接之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.