Async & await
11069 ワード
async
async function helloAsync() {
return 'hello Async';
}
helloAsync().then((res) => {
console.log(res) // 'hello Async'
})
上のコードがpromiseを使ってsettimeoutを使うとしたら?function delay(ms) {
return new Promis((res) => {
setTimeout(res, ms)
})
}
async function helloAsync() {
return delay(3000).then(() => {
return 'hello Async';
})
}
helloAsync().then((res) => {
console.log(res) // 3초 후에 'hello Async'
})
await
上のコードawaitを使うとしたら?
function delay(ms) {
return new Promis((res) => {
setTimeout(res, ms)
})
}
async function helloAsync() {
await delay(3000); //간단해짐
return 'hello Async';
}
helloAsync().then((res) => {
console.log(res) // 3초 후에 'hello Async'
})
(非同期関数遅延終了後に「hello async」を返す)
->helloAsync()呼び出しもasyncに変更した場合?
function delay(ms) {
return new Promis((res) => {
setTimeout(res, ms)
})
}
async function helloAsync() {
await delay(3000); //간단해짐
return 'hello Async';
}
async function main() {
const res = await helloAsync();
console.log(res);
}
main();
jsonデータのインポート方法
async function getData() {
let rawResponse = await fetch('주소');
let jsonResponse = await rawResponse.json();
console.log(jsonResponse);
}
getData();
asyncでawaitを使用して順序付け공부하며 정리&기록하는 ._. 씅로그
Reference
この問題について(Async & await), 我々は、より多くの情報をここで見つけました https://velog.io/@sseung-i/Async-awaitテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol