TypeScript5.2引入新關(guān)鍵字using介紹
TypeScript新關(guān)鍵字
TypeScript 5.2將引入一個(gè)新的關(guān)鍵字:using。當(dāng)它離開作用域時(shí),你可以用Symbol.dispose函數(shù)來處置任何東西。
{
const getResource = () => {
return {
[Symbol.dispose]: () => {
console.log('Hooray!')
}
}
}
using resource = getResource();
} // 'Hooray!' logged to console這是基于TC39提議,該提議最近達(dá)到了第三階段,表明它即將進(jìn)入JavaScript。
using將對(duì)管理文件句柄、數(shù)據(jù)庫連接等資源非常有用。
Symbol.dispose
Symbol.dispose是JavaScript中一個(gè)新的全局symbol。任何具有分配給Symbol.dispose函數(shù)的東西都將被視為"資源":也就是具有特定生命周期的對(duì)象。并且該資源可以使用using關(guān)鍵字。
const resource = {
[Symbol.dispose]: () => {
console.log("Hooray!");
},
};await using
你也可以使用Symbol.asyncDispose和await來處理那些需要異步處置的資源。
const getResource = () => ({
[Symbol.asyncDispose]: async () => {
await someAsyncFunc();
},
});
{
await using resource = getResource();
}這將在繼續(xù)之前等待Symbol.asyncDispose函數(shù)。
這對(duì)數(shù)據(jù)庫連接等資源來說很有用,你要確保在程序繼續(xù)前關(guān)閉連接。
使用案例
文件句柄
通過節(jié)點(diǎn)中的文件處理程序訪問文件系統(tǒng),使用using可能會(huì)容易得多。
不使用using:
import { open } from "node:fs/promises";
let filehandle;
try {
filehandle = await open("thefile.txt", "r");
} finally {
await filehandle?.close();
}使用using:
import { open } from "node:fs/promises";
const getFileHandle = async (path: string) => {
const filehandle = await open(path, "r");
return {
filehandle,
[Symbol.asyncDispose]: async () => {
await filehandle.close();
},
};
};
{
await using file = getFileHandle("thefile.txt");
// Do stuff with file.filehandle
} // Automatically disposed!數(shù)據(jù)庫連接
管理數(shù)據(jù)庫連接是在C#中使用using的一個(gè)常見用例。
不使用using:
const connection = await getDb();
try {
// Do stuff with connection
} finally {
await connection.close();
}使用using:
const getConnection = async () => {
const connection = await getDb();
return {
connection,
[Symbol.asyncDispose]: async () => {
await connection.close();
},
};
};
{
await using { connection } = getConnection();
// Do stuff with connection
} // Automatically closed!圖片示例
下圖是上面示例的圖片版本:

總結(jié)
本文簡要介紹了TypeScript5.2中引入的新關(guān)鍵字using,它的出現(xiàn)可以很好的和Symbol.dispose搭配使用。有了它我們便不需要在try…catch語句中進(jìn)行數(shù)據(jù)庫的關(guān)閉,這對(duì)管理文件句柄、數(shù)據(jù)庫連接等資源時(shí)非常有用。
更多關(guān)于TypeScript新關(guān)鍵字的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JS實(shí)現(xiàn)簡單的操作桿旋轉(zhuǎn)示例詳解
這篇文章主要為大家介紹了JS實(shí)現(xiàn)簡單的操作桿旋轉(zhuǎn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
輸入框跟隨文字內(nèi)容適配寬實(shí)現(xiàn)示例
這篇文章主要為大家介紹了輸入框跟隨文字內(nèi)容適配寬實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
微信小程序?qū)崿F(xiàn)拖拽 image 觸摸事件監(jiān)聽的實(shí)例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)拖拽 image 觸摸事件監(jiān)聽的實(shí)例的相關(guān)資料,這里提供image觸摸并監(jiān)聽的簡單實(shí)例,需要的朋友可以參考下2017-08-08
使用preload預(yù)加載頁面資源時(shí)注意事項(xiàng)
本文主要介紹 preload 的使用,以及與 prefetch 的區(qū)別。然后會(huì)聊聊瀏覽器的加載優(yōu)先級(jí),大家一定要認(rèn)真看完2020-02-02
mitt tiny-emitter發(fā)布訂閱應(yīng)用場(chǎng)景源碼解析
這篇文章主要為大家介紹了mitt tiny-emitter發(fā)布訂閱應(yīng)用場(chǎng)景源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
詳解微信小程序如何實(shí)現(xiàn)類似ChatGPT的流式傳輸
這篇文章主要為大家介紹了微信小程序如何實(shí)現(xiàn)類似ChatGPT的流式傳輸示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

