NODEJS javascript CALL BACK,PROMISE

CALL BACK과 PROMISE에 대해 간단히 살펴 봅니다.
이 CALL BACK과 PROMISE는 함수의 순차적 실행과 관계가 있습니다.

Let’s take a quick look at CALL BACK and PROMISE.
These CALL BACK and PROMISE are related to the sequential execution of functions.

1.CALL BACK

1)함수만들기Create a function

함수 add의 아규먼트로 x, callback_func를 사용합니다.
x는 변수이고 callback_func은 함수인데 이렇게 아규먼트로 함수를 사용하는 것을 call back 함수라고 합니다.
add 함수내에 callback_func(x)로 실행됩니다.
이때 아규먼트가 하나인 함수가 실행되는데 아규먼트로 x가 callback_func의 아규먼트로 대입됩니다.
함수의 아규먼트 갯수까지만 설정되었고 나머지 자세한 내용은 add 함수가 실행 될 때 함수를 정의 합니다.

Use x, callback_func as the argument for the function add.
x is a variable and callback_func is a function, so using a function as an a argument is called a callback function.
Runs as callback_func(x) within add function
At this time, a function with one argument is executed, and x is substituted as the argument of callback_func.
Only the number of arguments in the function is set, and the remaining details are defined when the add function is executed.

function add(x, callback_func) {
  callback_func(x);
}

2)함수 실행(Function execution)

add 함수를 실행할 때 값으로 “test”라는 x변수의 값이 대입되고 callback_func위치에 실제 함수를 그대로 대입합니다.
이때 완전한 함수를 설정합니다.
callback_func(x)라고 아규먼트를 하나로 정해 줬으니 아규먼트가 하나인 함수를 정의 합니다.
이 함수는 이름이 없고 result에 대입되는 내용은 console.log(resujlt)로 콘솔에 출력됩니다.
위에서 callback_func(x)이기 때문에 “test”는 result변수의 값이 되어 함수가 실행됩니다.

When executing the add function, the value of the x variable called “test” is assigned as the value, and the actual function is assigned to the callback_func position.
At this point, we set up the complete function.
Since we have set one argument as callback_func(x), we define a function with one argument.
This function has no name, and the content substituted into result is output to the console as console.log(resujlt).Since callback_func(x) above, “test” becomes the value of the result variable and the function is executed.

add("test",function(result){
  console.log(result);
});

3)전체코드(full code)

function add(x, callback_func) {
  callback_func(x);
}

add("test",function(result){
  console.log(result);
});

2.PROMISE

1)PROMISE만들기(Create a PROMISE) & resolve()

아래는 resolve와reject를 아규먼트로 사용하는 PROMISE를 만드는 코드입니다.
resolve와 reject는 함수입니다.
PROMISE내부의 모든 함수가 정상적으로 실행되면 resolve함수를 실행하여 그 다음 함수를 실행할 수 있습니다.
다음 함수란 then(()=>{ function }) 코드 내의 함수를 의미합니다.

Below is the code that creates a PROMISE using resolve and reject as arguments.
resolve and reject are functions.
When all functions within PROMISE are executed normally, the resolve function can be executed to execute the next function.
The next function means the function used in the block within then(()=>{ …function …})

const p = new Promise((resolve,reject)=>{
 resolve("resolve");
... CODE ...
})
p.then(()=>{
  console.log(2);
})

2)PROMISE reject()

reject()함수가 실행되면 .catch(()=>{… function …}) 블럭내의 함수가 실행되고 바로 이전의 then(()=>{…function…})블럭의 코드 실행이 취소됩니다.

When the reject() function is executed, the function in the .catch(()=>{… function …}) block is executed and the immediately preceding then(()=>{…function…}) block The code execution is canceled.

const p = new Promise((resolve,reject)=>{
   reject("reject");
});

p.then(()=>{
  console.log(2);
})
.catch(()=>{
  console.log("reject!");
})

3)전체코드(full code)

let se = 2;
const p = new Promise((resolve,reject)=>{
console.log(1);
  if(se == 1){
    resolve("resolve");
  }else{
    reject("reject");
  }
});

p.then(()=>{
  console.log(2);
})
.catch(()=>{
  console.log("reject!");
})
.then(()=>{
  console.log(3);
})
.then(()=>{
  console.log(4);
});

Leave a Reply

Your email address will not be published. Required fields are marked *