Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

⚡ async - Asenkron Programlama

async/await, Futures, Tasks, Runtime

71
Fonksiyon
480
Satır
13 KB
Boyut

🚀 Hızlı Başlangıç

içe_aktar async, http

// Async function
async fonksiyon veri_getir(url: yazı) -> Sonuç yap
    değişken yanıt = await http.get(url)
    
    eğer yanıt.durum != 200 ise yap
        dön Hata("HTTP " + yanıt.durum.yazıya())
    son
    
    dön Tamam(yanıt.gövde)
son

// Parallel execution
async fonksiyon ana() yap
    değişken görevler = [
        veri_getir("https://api.example.com/users"),
        veri_getir("https://api.example.com/posts"),
        veri_getir("https://api.example.com/comments")
    ]
    
    değişken sonuçlar = await async.all(görevler)
    
    her sonuç içinde sonuçlar için yap
        yazdır(sonuç)
    son
son

async.run(ana())

📚 Özellikler

  • async/await: Kolay asenkron kod
  • Futures: Lazy evaluation
  • Tasks: Concurrent execution
  • Runtime: Task scheduler

💡 Örnekler

Async HTTP Server

içe_aktar async, http

async fonksiyon kullanıcı_getir(id: tamsayı) -> Sonuç yap
    await async.sleep(100)  // Simulate DB query
    dön Tamam({"id": id, "name": "User " + id.yazıya()})
son

async fonksiyon ana() yap
    değişken sunucu = http.AsyncServer_new(3000)
    
    sunucu.rotala("GET", "/api/user/:id", async fonksiyon(istek, yanıt) yap
        değişken id = istek.params["id"].parse_int()
        değişken user = await kullanıcı_getir(id)
        
        eşleştir user yap
            Tamam(data) => yanıt.json(data),
            Hata(e) => yanıt.durum(500).json({"error": e})
        son
    son)
    
    yazdır("Async sunucu http://localhost:3000")
    await sunucu.başlat()
son

async.run(ana())

← network | Standart Kütüphane →