728x90
반응형
* Modern Javascript 기능을 100% 사용할 수 있게해주는 기능
Polyfill이란?
- JS standard library에 표준으로 등록되어 있으나, 아직 브라우저나 Node.js에서 구현되지 않은 기능을 미리 써볼 수 있도록 만들어진 구현체를 뜻한다.
ex) core.js
참고 사이트 : https://github.com/zloirock/core-js
* 코드1
// @ts-check
//core-js를 가져오는 부분
//처음 셋업 해주는 시간이 있어 실행 시 시간이 조금 걸린다.
require('core-js')
const complicateArray = [1, [2, 3]]
const flattendArray = complicateArray.flat()
console.log(flattendArray)
* 코드 2
// @ts-check
require('core-js')
const original = 'abcabc123'
const changed = original.replaceAll('abc', '123')
console.log(changed)
/**
*
* @param {number} duration
*/
function sleep(duration) {
return new Promise((resolve) => {
console.log('sleep start')
setTimeout(() => {
console.log('sleep done', duration)
resolve(duration)
}, duration)
})
}
function alwaysRejct() {
return new Promise((resolve, reject) => {
reject()
})
}
//모두가 resolve 되야만 가능
//promise.all
/**
Promise.all([sleep(1000), sleep(1000), sleep(1000)]).then((value) => {
console.log('Promise.all done!', value)
})
*/
//Reject 같이 넣을 때
/**
Promise.all([sleep(1000), sleep(1000), sleep(1000), alwaysRejct()]).then(() => {
console.log('Promise.all done!')
})*/
//allSettled는 resolve가 됬든 reject가 됬든 모두가 settle되면 끝을 내고 then으로 넘어가라
Promise.allSettled([sleep(1000), sleep(1000), sleep(1000), alwaysRejct()]).then(
(value) => {
console.log('Promise.allSettled done!', value)
}
)
728x90
반응형
'Node.js' 카테고리의 다른 글
간단한 http 서버 생성 & url 체크 (0) | 2021.09.07 |
---|---|
Transpile (0) | 2021.09.06 |
async _ await (0) | 2021.09.06 |
Promise (0) | 2021.09.06 |
JavaScript 문제 풀기 (0) | 2021.09.05 |
댓글