استدعاء غير متزامن من غير متزامن
We have a “regular” function called f
. How can you call the async
function wait()
and use its result inside of f
?
async function wait() { await new Promise((resolve) => setTimeout(resolve, 1000)); return 10; } function f() { // ...what should you write here? // we need to call async wait() and wait to get 10 // remember, we can't use "await" }
ملاحظة. المهمة بسيطة من الناحية الفنية ، ولكن السؤال شائع جدًا للمطورين الجدد على عدم المزامنة / الانتظار.
هذا هو الحال عند معرفة كيف يعمل في الداخل مفيد.
ما عليك سوى التعامل مع استدعاء غير متزامن '' على أنه وعد وإرفاق
ثم ‘’ به:
async function wait() { await new Promise(resolve => setTimeout(resolve, 1000)); return 10; } function f() { // shows 10 after 1 second wait().then(result => alert(result)); } f();