Giter VIP home page Giter VIP logo

2019_node.js_study's Introduction

2019_node.js_study's People

Contributors

pmy0792 avatar

Watchers

James Cloos avatar Juwon Seo avatar  avatar

2019_node.js_study's Issues

Assignment 2 feedback

(setTimeout(), https://nodejs.org/ko/docs/guides/timers-in-node/#settimeout)

const list=[1,2,3,4,5,6,7,8,9,10];

function callbackExample(items,callback){
    setTimeout(function(){
        let sum=0;
        for(let i=0;i<items.length;i++){
            sum+=items[i];
        }
        callback(sum);
    },0);
};
callbackExample(list,function(result){
    console.log(result); //두번째 출력
});
console.log('first'); //첫번째 출력

setTimeout() 함수는 주어진 시간(ms)이후에 주어진 함수를 실행하는 기능을 합니다.

1.Introduction에서 언급한 것 처럼 Node.js는 비동기 프로그래밍을 지원합니다.
callback은 비동기 프로그래밍에서 중요한 역할을 합니다.
setTimeout()은 위 예제에서 시간이 오래 걸리는 작업을 의미합니다.
처음에 callbackExample이 호출 되었을 때 setTimeout에 의해 프로그램은
0ms이긴 하지만 주어진 시간동안 기다리라는 명령을 받습니다.
그 동안 callbackExample이 호출되었던 다음 문장인 console.log('first');를 실행합니다.
이것이 비동기 프로그래밍의 장점입니다.
setTimeout에 10분이 걸려있다고 한들 10분동안 그것만 기다릴 수는 없죠.
console.log('first');를 실행하고 나니 주어진 시간이 경과했습니다.
리스트내의 모든 아이템의 합을 구했고, 이를 출력하는 callback함수를 전달해 주었습니다.
만약에 callback함수를 사용하지 않았다면,

callbackExample(list);
console.log('first');
console.log(result);

이런식으로 결과를 출력하고자 했다면, result를 출력하는 시점이 callbackExample이 수행된 이후라는 것을 보장할수가 없습니다.
그렇기에 callback 함수를 사용한 것입니다.
함수 내부적으로 callbackExample 수행중에 result를 출력하는 함수를 호출한것입니다.
result를 출력하는 함수가 호출되기 위해서는 callbackExample이 우선 수행되어야 하니까
result를 실행시키는 시점은 보장된 것이죠.

여기까지가 setTimeout을 사용한 이유였습니다.
제출한 과제에서 setTimeout 함수 부분만 따로 추가해서 커밋해 주세요.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.