https://developers.google.com/web/fundamentals/primers/async-functions?hl=ko
async function logFetch(url) {
try {
const response = await fetch(url);
console.log(await response.text());
}
catch (err) {
console.log('fetch failed', err);
}
}
await하는 것은 전부 Promise.resolve()를 통해 전달되므로, 기본 프라미스가 아닌 프라미스를 안전하게 await할 수 있습니다.
// wait ms milliseconds
function wait(ms) {
return new Promise(r => setTimeout(r, ms));
}
async function hello() {
await wait(1500);
return 'world';
}
hello()
.then(function(r,j){
console.info(r,j);
})
.catch(function(e){
console.info(e);
});
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
// wait ms milliseconds
function wait(ms) {
return new Promise(r => setTimeout(r, ms));
}
async function foo() {
// await wait(500);
throw Error('bar');
}
async function foo() {
// await wait(500);
throw {"aError":"aError"}
}
foo()
.then(function(r,j){
console.info(r,j);
})
.catch(function(e){
console.info(e);
});
'web > javascript' 카테고리의 다른 글
react class componet (0) | 2022.04.04 |
---|---|
Promise then (0) | 2021.10.01 |
simple padding zero (0) | 2020.02.27 |
Javascript SnakBar (0) | 2019.08.06 |
show loading progress image (0) | 2019.08.05 |
댓글