저는 node.js를 처음 사용하며 자바 스크립트를 처음 사용했습니다. 콜백이 작동하는 방식을 이해하고 직접 함수를 시험해보고 싶었습니다.node.js의 콜백 함수
MyScript.js : 여기 내 코드입니다
var calledfunction = function()
{
console.log("This is a called function");
for(i=0;i<1090660;i++)
{
console.log(i);
}
console.log('done');
};
var sayHello = require('./sayhello.js');
objhello = new sayHello();
objhello.setupSuite(1,calledfunction);
console.log('Next statement;');
sayhello.js 위의 프로그램을 실행하여 실행
var _ = require('underscore');
module.exports = exports = CLITEST;
function CLITEST(param1,param2)
{
}
_.extend(CLITEST.prototype, {
setupSuite: function (here,callback) {
console.log(here);
console.log('This is a callback function');
callback();
}
})
> 노드 Myscript.js는
내 질문은 : for 루프는 콘솔에서 모든 숫자를 실행하고 인쇄하는 데 50 초를 소비하고 th en은 콜백 함수 외부에있는 "Next statement"행만을 실행합니다.
왜 이런 일이 발생합니까? 즉각적인 성명서는 기능이 실행될 때까지 기다리지 않고도 실행된다는 이론을 읽으므로 말입니다.
이상적인 출력 된해야 인쇄 "다음 문"다음 루프
의 내용을 인쇄하지만 위의 경우는 그 반대의 경우도 마찬가지입니까?
코드 서식을 수정하려고합니다. 읽기가 어렵습니다. – codebox
코드에 비동기 함수가 없습니다. 먼저 '다음 문'을 먼저 인쇄해야합니까? – thefourtheye