Skip to content

Instantly share code, notes, and snippets.

@xodhx4
xodhx4 / Markdium-javascript.js
Created April 2, 2020 11:29
Markdium-JS 초보가 쓰는 async await 처음부터 이해하기
function returnResult() { // 안됨
const result = await callApi();
return result + 10;
}
@xodhx4
xodhx4 / Markdium-javascript.js
Created April 2, 2020 11:29
Markdium-JS 초보가 쓰는 async await 처음부터 이해하기
async function printResult() {
const data1 = wait callApi1(); // 5초 걸림
const data2 = wait callApi2(); // 3초 걸림
console.log(data1, data2); // 8초 걸림
}
@xodhx4
xodhx4 / Markdium-javascript.js
Created April 2, 2020 11:29
Markdium-JS 초보가 쓰는 async await 처음부터 이해하기
async function printResultConcurrent() {
const data = wait Promise.all([callApi1(), callApi2()]); // 둘다 동시에 기다리기 때문에 가장 긴 5초를 대기
console.log(data); // 5초 걸림
}
@xodhx4
xodhx4 / Markdium-python.py
Created April 2, 2020 11:29
Markdium-JS 초보가 쓰는 async await 처음부터 이해하기
from concurrent.futures import *
with ThreadPoolExecutor(max_workers=1) as excutor:
future = excutor.submit(open, "example.txt", "r") // example.txt를 다 읽을 때 까지 기다리지 않고 Future 객체를 돌려준다 : Non-Blocking IO
f = future.result() // future에게 example.txt 의 파일 객체를 달라고 요청한다. example.txt를 다 읽을 때 까지 기다린다 : Blocking IO
print(f.readline())
@xodhx4
xodhx4 / Markdium-javascript.js
Created April 2, 2020 11:29
Markdium-JS 초보가 쓰는 async await 처음부터 이해하기
async function returnResult() { // Promise를 리턴
const result = await callApi();
return result + 10;
}
function returnResult() { // Promise를 리턴
return callApi()
.then(function(result) {
return result + 10
});
@xodhx4
xodhx4 / Markdium-javascript.js
Created April 2, 2020 11:29
Markdium-JS 초보가 쓰는 async await 처음부터 이해하기
printAsync1().then(function(result) {
console.log(result);
}
printAsync2().then(function(result) {
console.log(result);
}
@xodhx4
xodhx4 / Markdium-javascript.js
Created April 2, 2020 11:29
Markdium-JS 초보가 쓰는 async await 처음부터 이해하기
async function alertResult() {
// Blocking IO처럼 보이게 한다.
const result = await callApi(); // callApi는 async 함수이거나, promise를 리턴하는 함수다.
console.log(result); // 결과를 출력한다
}
@xodhx4
xodhx4 / Markdium-python.py
Created April 2, 2020 11:29
Markdium-JS 초보가 쓰는 async await 처음부터 이해하기
future = excutor.submit(open, "example.txt", "r") // Non-Blocking IO로 IO 작업 시작
some_cpu_job1() // cpu 작업이 IO작업이 끝나기 전에 끝난다
some_cpu_job2() // 이 시점에서 IO 작업이 끝났다면
some_cpu_job3() // 그럼에도 불구하고 이 cpu 작업을 기다려야 한다
f = future.result() // 출력이 Blocking일 때보다 늦어진다 -> 응답성이 떨어진다
print(f.readline())
@xodhx4
xodhx4 / Markdium-python.py
Created April 2, 2020 11:29
Markdium-JS 초보가 쓰는 async await 처음부터 이해하기
future = excutor.submit(open, "example.txt", "r") // Non-Blocking IO로 IO 작업 시작
some_cpu_job1() // cpu 작업을 실행한다. example.txt는 필요 없는 작업
some_cpu_job2() // 이 때 IO가 example.txt를 읽는 작업을 하고있지만 동시에 cpu 작업을 한다
f = future.result() // 상품값을 가져온다
print(f.readline()) // result의 결과값이 필요하기 때문에 항상 future.result 뒤에 실행
@xodhx4
xodhx4 / Markdium-javascript.js
Created April 2, 2020 11:29
Markdium-JS 초보가 쓰는 async await 처음부터 이해하기
function returnDataAfter2Seconds() { // 2초 뒤에 값을 돌려주는 IO 작업
return new Promise(resolve => { // 2초 동안 Blocking 되지 않기 위해 Promise 객체를 return 한다
setTimeout(() => {
resolve('data');
}, 2000);
});
}
f = returnDataAfter2Seconds();
const dataExpected = f.then((data) => {return data;}); // 결과값을 돌려받기 기대한다