function execute (someFunction, value) {
console.log("before");
someFunction(value);
console.log("after");
}execute(function(value){console.log(value);}, 'hi js.');也是网上看了,自己写了这个例子,那不是说回调函数是在execute函数结束完之后再执行someFunction这个回调函数吗?那为什么执行的结果永远是
before
hi js
after这样按顺序执行呢?如果按顺序,那不就是就函数嵌套函数么,我的意思是,如果execute 就是页面的document.ready方法,那如何来保证someFunction是在最后一步执行的呢?求大神开导
console.log("before");
someFunction(value);
console.log("after");
}execute(function(value){console.log(value);}, 'hi js.');也是网上看了,自己写了这个例子,那不是说回调函数是在execute函数结束完之后再执行someFunction这个回调函数吗?那为什么执行的结果永远是
before
hi js
after这样按顺序执行呢?如果按顺序,那不就是就函数嵌套函数么,我的意思是,如果execute 就是页面的document.ready方法,那如何来保证someFunction是在最后一步执行的呢?求大神开导
的形式,而你这个代码却是同步的调用回调函数,没有异步方法。
要回调函数最后执行可以用定时器模拟异步回调
function execute (someFunction, value) {
console.log("before");
setTimeout(function(){
someFunction(value);
}, 50);
console.log("after");
}
console.log("before");
console.log("after");
return new promise(value);
}execute('hi js.').then(someTimeout);