Svelte Await 塊

2023-02-21 13:51 更新

很多 Web 應(yīng)用程序都可能在某個時候有需要處理異步數(shù)據(jù)的需求。使用 Svelte 在標(biāo)簽中使用 await 處理 promise 數(shù)據(jù)亦是十分容易:

{#await promise}
	<p>...waiting</p>
{:then number}
	<p>The number is {number}</p>
{:catch error}
	<p style="color: red">{error.message}</p>
{/await}

?promise?總是獲取最新的信息,這使得你無需關(guān)心 rece 狀態(tài)。

如果你的知道你的 promise 返回錯誤,則可以忽略 catch 塊。如果在請求完成之前不想程序執(zhí)行任何操作,也可以忽略第一個塊。

{#await promise then value}
	<p>the value is {value}</p>
{/await}


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號