對於async和await的理解

2021-09-26 15:23:56 字數 3246 閱讀 5395

直接先上**

上結果:

testsync.controllers.valuescontroller:information: 4----------------8/29/2019 2:03:26 pm

testsync.controllers.valuescontroller:information: 9***************x8/29/2019 2:03:29 pm

testsync.controllers.valuescontroller:information: 5***************x8/29/2019 2:03:29 pm

testsync.controllers.valuescontroller:information: 5***************x8/29/2019 2:03:32 pm

testsync.controllers.valuescontroller:information: 4***************x8/29/2019 2:03:35 pm

testsync.controllers.valuescontroller:information: 4+++++++++++++++++8/29/2019 2:03:35 pm

testsync.controllers.valuescontroller:information: 4----------------8/29/2019 2:03:57 pm

testsync.controllers.valuescontroller:information: 9***************x8/29/2019 2:04:00 pm

testsync.controllers.valuescontroller:information: 5***************x8/29/2019 2:04:00 pm

testsync.controllers.valuescontroller:information: 4***************x8/29/2019 2:04:04 pm

testsync.controllers.valuescontroller:information: 4***************x8/29/2019 2:04:07 pm

testsync.controllers.valuescontroller:information: 4+++++++++++++++++8/29/2019 2:04:07 pm

testsync.controllers.valuescontroller:information: 5----------------8/29/2019 2:04:35 pm

testsync.controllers.valuescontroller:information: 4***************x8/29/2019 2:04:38 pm

testsync.controllers.valuescontroller:information: 9***************x8/29/2019 2:04:38 pm

testsync.controllers.valuescontroller:information: 4***************x8/29/2019 2:04:42 pm

testsync.controllers.valuescontroller:information: 4***************x8/29/2019 2:04:45 pm

testsync.controllers.valuescontroller:information: 4+++++++++++++++++8/29/2019 2:04:45 pm

testsync.controllers.valuescontroller:information: 15----------------8/29/2019 2:05:08 pm

testsync.controllers.valuescontroller:information: 5***************x8/29/2019 2:05:10 pm

testsync.controllers.valuescontroller:information: 4***************x8/29/2019 2:05:11 pm

testsync.controllers.valuescontroller:information: 14***************x8/29/2019 2:05:11 pm

testsync.controllers.valuescontroller:information: 5***************x8/29/2019 2:05:13 pm

testsync.controllers.valuescontroller:information: 5+++++++++++++++++8/29/2019 2:05:13 pm

說下我自己的理解當執行到await的時候,之前的那個執行緒會被**掉,執行await的時候,又會建立乙個新的執行緒,但是裡面機制是盡量用之前**過得執行緒。

此模型可很好地處理典型的伺服器方案工作負荷。 由於沒有專用於阻止未完成任務的執行緒,伺服器執行緒池可服務更多的 web 請求。

考慮使用兩個伺服器:乙個執行非同步**,乙個不執行非同步**。 鑑於本示例的目的,每個伺服器只有 5 個可用於服務請求的執行緒。 請注意,這樣小的數目僅可用於演示。

假設兩個伺服器都接收到 6 個併發請求。 每個請求執行乙個 i/o 操作。 未 執行非同步**的伺服器必須對第 6 個請求排隊,直到 5 個執行緒中的乙個完成了繫結 i/o 的工作並編寫了響應。 此時收到了第 20 個請求,由於佇列過長,伺服器可能會開始變慢。

執行有 非同步**的伺服器也需對第 6 個請求排隊,但由於使用了 async 和 await,繫結了 i/o 的工作開始時,每個執行緒都會得到釋放,無需等到工作結束。 收到第 20 個請求時,傳入請求佇列將變得很小(如果其中還有請求的話),且伺服器不會變慢。

儘管這是乙個人為想象的示例,但在現實世界中其工作方式與此類似。 事實上,相比伺服器將執行緒專用於接收到的每個請求,使用 async 和 await 能夠使伺服器多處理乙個數量級的請求。

我之前理解的非同步的話,我實踐下來需要定義乙個非同步方法,然後呼叫的時候不通過關鍵字await阻塞,這樣程式會繼續往下執行,可以適用一些寫入日誌的操作。

以上的純屬個人的看法,希望大家指正。

async和await的講解

普通的函式宣告 async function a 複製 宣告乙個函式表示式 let a async function 複製 async形式的箭頭函式 let a async 複製 async與await例項應用,基礎 控制器呼叫與server中查詢資料 exports.getbloglist asy...

async和await的使用

async其實是es7的才有的,是非同步操作的進化,其實就是封裝乙個promise的物件返回 async function test console.log test promiseasync方法在普通的函式前加上 async 關鍵字即可。執行這個函式,發現並沒有返回1111,而是通過promise...

async和await的講解

async和await的講解 宣告async函式的幾個方法 普通的函式宣告 async function a 宣告乙個函式表示式 let a async function async形式的箭頭函式 let a async 初識async和await async與await例項應用,基礎 控制器呼叫與...