Is there a way to short circuit async/await flow?

前端 未结 7 2080
名媛妹妹
名媛妹妹 2020-12-02 11:19
async function update() {
   var urls = await getCdnUrls();
   var metadata = await fetchMetaData(urls);
   var content = await fetchContent(metadata);
   await rend         


        
7条回答
  •  日久生厌
    2020-12-02 11:44

    This answer I posted may help you to rewrite your function as:

    async function update() {
       var get_urls = comPromise.race([getCdnUrls()]);
       var get_metadata = get_urls.then(urls=>fetchMetaData(urls));
       var get_content = get_metadata.then(metadata=>fetchContent(metadata);
       var render = get_content.then(content=>render(content));
       await render;
       return;
    }
    
    // this is the cancel command so that later steps will never proceed:
    get_urls.abort();
    

    But I am yet to implement the "class-preserving" then function so currently you have to wrap every part you want to be able to cancel with comPromise.race.

提交回复
热议问题