現在の言語: 日本語 |
await |
同期非同期 |
async function test()
{
// レスポンスが返ってくるまでここで待機する
const response = await fetch("https://api.example.com/data");
// JSONの解析が終わるまで待機する
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const img = document.getElementById("img");
img.src = url;
}
test();
/*
[try...catchの例]
awaitのエラーはtry..catchを使用するのが一般的です。
*/
async function test2()
{
try
{
// レスポンスが返ってくるまでここで待機する
const response = await fetch("https://api.example.com/data");
// JSONの解析が終わるまで待機する
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const img = document.getElementById("img");
img.src = url;
}
catch(error)
{
console.log("error:"+error);
}
}
test2();
async function test3()
{
f1();
f2();
await f3();
f4();
}
function f1()
{
...
}
async function f2()
{
...
}
async function f3()
{
...
}
async function f4()
{
...
}
async function test()
{
// Wait here until the response is returned.
const response = await fetch("https://api.example.com/data");
// Wait until JSON parsing is complete.
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const img = document.getElementById("img");
img.src = url;
}
test();
/*
[try...catch example]
It is common to use try.catch to handle await errors.
*/
async function test2()
{
try
{
// Wait here until the response is returned.
const response = await fetch("https://api.example.com/data");
// Wait until JSON parsing is complete.
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const img = document.getElementById("img");
img.src = url;
}
catch(error)
{
console.log("error:"+error);
}
}
test2();
async function test3()
{
f1();
f2();
await f3();
f4();
}
function f1()
{
...
}
async function f2()
{
...
}
async function f3()
{
...
}
async function f4()
{
...
}
| 著作権情報 |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |