ҳ > ̳̺ϼ > Html/css >

ΪʲôҪʹ async/await

ʱ䣺2021-06-02 Ķ0

async/await ES7 ı׼Promise ES6 ׼async/await API Ҳд첽ģǹ Promise ֮ϵģе Okhttp Retrofit Ĺϵ

ΪʲôҪʹ async/await

ʲô async


async function myFirstAsyncFunction() {
    try {
        const fulfilledValue = await doSomeThing();
    }
    catch (rejectedValue) {
        // …
    }
}
 

ųӡasync һ㲻ʹãǺ await һʹãһ async ڲ await

δ뼴ʹûѧ Promise Ҳ׿ async ڡ

async õʱ򣬻һ Promise

await

await ܵʹãڷ async ڲûᱨawait һһ Promise Ҳģһֵһһ await 治һ Promise ͻ᷵һѾ resolve Promise

async ִе await ʱ򣬻ͣasyncִн̲ȨֻеȴĻPromise 첽ֻ򱻾ܾ֮Żָ̡

Ȼasync Ҳ᷵һ Promise Ҳ˵await ҲԸһ async

async/await API о Kotlin е Э ζ

ΪʲôҪʹ async

Promise

һӿڣȻӦݴӡҲ쳣 Promise д


function logFetch(url) {
    return fetch(url)
        .then(response => response.text())
        .then(text => {
            console.log(text);
        }).catch(err => {
            console.error('fetch failed', err);
        });
}
 

async дӣ


async function logFetch(url) {
    try {
        const response = await fetch(url);
        console.log(await response.text());
    }
    catch (err) {
        console.log('fetch failed', err);
    }
}
 

Ȼ࣬Ǵ뿴Ӽ࣬˺ܶ then ǶסһӿݣȻӡ㿴ģܼ򵥡

ղŵǸӣûѧ Promise ҲӰ㿴 async 롣ûʲôǶһ͵ĿԱļˮƽβ룬ܱ֤˶Ϥ Promise ҪǾܵĽʹѶȣʹ󲿷˶ܿ

ͬ˼·д첽߼

async/await ƾǿͬ˼·д첽ҵ߼Դ忴׿Ǿٸӡ

Ĵ뻹DZȽϼ򵥣پһһӡ

ȡһԴĴСʹ Promise ſӣ


function getResponseSize(url) {
    return fetch(url).then(response => {
        const reader = response.body.getReader();
        let total = 0;

        return reader.read().then(function processResult(result) {
            if (result.done) return total;

            const value = result.value;
            total += value.length;
            console.log('Received chunk', value);

            return reader.read().then(processResult);
        })
    });
}
 

ʹѧ Promise ҲǺܺ⣬˵ûѧ Promise ͬѧˡΪмһѭḶִ́еĹ̵첽ģ֮ǰѧһʽܽġȻҲԳȡһ£ʹһ㡣


const processResult = (result) =>{
            if (result.done) return total;

            const value = result.value;
            total += value.length;
            console.log('Received chunk', value);

            return reader.read().then(processResult);
}

function getResponseSize(url) {
    return fetch(url).then(response => {
        const reader = response.body.getReader();
        let total = 0;

        return reader.read().then(processResult)
    });
}
 

Ƕһǿǿɡǿһ async ôġ


async function getResponseSize(url) {
    const response = await fetch(url);
    const reader = response.body.getReader();
    let result = await reader.read();
    let total = 0;

    while (!result.done) {
        const value = result.value;
        total += value.length;
        console.log('Received chunk', value);
        // get the next result
        result = await reader.read();
    }

    return total;
}
 

Dzǿ͸ˣΪ await ʽУֱѭ忴ͬĴ룬Ҳֱ׶롣

С await

await ܹ async УԴ뿴ͬĴ룬Ķ⡣ҪС await ΪЩDzҪģǡʹÿܻӰܡ

Ҫһݺͱݺϲʵӣ


async function combineData(url, file) {
        let networkData = await fetch(url)
        let fileData = await readeFile(file)
        console.log(networkData + fileData)
}
 

ĻͿͬʱ ȡļ ˣԽʡܶʱ䡣Ҫ Promise һִеص㣬ͬѧԸϰһһڵݡ

ȻϤ Promise Ļֱʹ Promise.all ķʽ await Promise.all Ͳչˡ

try...catch

async У쳣һ try...catch ûн try...catch await ʽһ reject async ص Promise ͻ reject

ʵ Promise һ Promise ״̬öΪ reject Һ then ûд reject û catch ôͻ׳쳣Ƕ async try...catch ס await ʽܾ catch ס쳣Ұ reject Ϣ catch 档

Ͳˡ

ͨȨȫվǧTBԴ >>> ԴĿ¼ 鿴Դ֡Ӱӡ顢顢γ̵ȣʱѣ >>> Ȩ ˽࣡

භභ

Ƽ

ض