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