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

前端 未结 7 2081
名媛妹妹
名媛妹妹 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:53

    Here is a simple exemple with a promise:

    let resp = await new Promise(function(resolve, reject) {
        // simulating time consuming process
        setTimeout(() => resolve('Promise RESOLVED !'), 3000);
        // hit a button to cancel the promise
        $('#btn').click(() => resolve('Promise CANCELED !'));
    });
    

    Please see this codepen for a demo

提交回复
热议问题