Async

# async function f() {
  //await Promise.reject('出错了');
   throw new Error('出错了');
}
 
 
async function getCaptchaCode(){
   let res=await f();
   console.log('----------------');
}
 
getCaptchaCode().then(suc=>console.log('对了'),error=>console.log('出错了'));
 
//出错了
=============================================================================
 
 async function f() {
  //await Promise.reject('出错了');
  throw new Error('出错了');
  //return await 2;
}
 
 
async function getCaptchaCode(){
   let res=await f();
   console.log('----------------');
}
 
getCaptchaCode().then(v=>console.log('--------suc')).catch(e=>console.log('---------------error'));
 
//---------------error
 
============================================================================
 
async function f() {
  //await Promise.reject('出错了');
   //throw new Error('出错了');
  return await 2;
}
 
 
async function getCaptchaCode(){
   let res=await f();
   console.log('----------------');
}
 
getCaptchaCode().then(v=>console.log('--------suc')).catch(e=>console.log('---------------error'));
 
//----------------
// --------suc
=================================================================================
 
var resolveAfter2Seconds = function() {
  console.log("starting slow promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve(20);
      console.log("slow promise is done");
    }, 2000);
  });
};
 
var resolveAfter1Second = function() {
  console.log("starting fast promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve(10);
      console.log("fast promise is done");
    }, 1000);
  });
};
 
var sequentialStart = async function() {
  console.log('==SEQUENTIAL START==');
 
  // 如果 await 操作符后的表达式不是一个 Promise 对象, 则它会被转换成一个 resolved 状态的 Promise 对象
  const slow = await resolveAfter2Seconds();
 
  const fast = await resolveAfter1Second();
  console.log(slow);
  console.log(fast);
}
sequentialStart();
 
// ==SEQUENTIAL START==
// starting slow promise
// Promise {<pending>}
// slow promise is done
// starting fast promise
// fast promise is done
// 20
// 10
=================================================================================
 
 
var resolveAfter2Seconds = function() {
  console.log("starting slow promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve(20);
      console.log("slow promise is done");
    }, 2000);
  });
};
 
var resolveAfter1Second = function() {
  console.log("starting fast promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve(10);
      console.log("fast promise is done");
    }, 1000);
  });
};
 
 
var concurrentStart = async function() {
  console.log('==CONCURRENT START with await==');
  const slow = resolveAfter2Seconds(); // 立即启动计时器
  const fast = resolveAfter1Second();
 
  console.log(await slow);
  console.log(await fast); // 等待 slow 完成, fast 也已经完成。
}
 
concurrentStart();
 
// ==CONCURRENT START with await==
// starting slow promise
// starting fast promise
// Promise {<pending>}
// fast promise is done
// slow promise is done
// 20
// 10
 
=================================================================================
 
 
var resolveAfter2Seconds = function() {
  console.log("starting slow promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve(20);
      console.log("slow promise is done");
    }, 2000);
  });
};
 
var resolveAfter1Second = function() {
  console.log("starting fast promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve(10);
      console.log("fast promise is done");
    }, 1000);
  });
};
 
var stillSerial = function() {
  console.log('==CONCURRENT START with Promise.all==');
  Promise.all([resolveAfter2Seconds(), resolveAfter1Second()]).then(([slow, fast]) => {
    console.log(slow);
    console.log(fast);
  });
}
 
stillSerial();
 
// ==CONCURRENT START with Promise.all==
// starting slow promise
// starting fast promise
// undefined
// fast promise is done
// slow promise is done
// 20
// 10
 
=================================================================================
 
var resolveAfter2Seconds = function() {
  console.log("starting slow promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve(20);
      console.log("slow promise is done");
    }, 2000);
  });
};
 
var resolveAfter1Second = function() {
  console.log("starting fast promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve(10);
      console.log("fast promise is done");
    }, 1000);
  });
};
 
var parallel = function() {
  console.log('==PARALLEL with Promise.then==');
  resolveAfter2Seconds().then((message)=>console.log(message)); // in this case could be simply written as console.log(resolveAfter2Seconds());
  resolveAfter1Second().then((message)=>console.log(message));
}
parallel();
 
// ==PARALLEL with Promise.then==
// starting slow promise
// starting fast promise
// undefined
// fast promise is done
// 10
// slow promise is done
// 20
=================================================================================
/*
不要将await和Promise.then混淆
在sequentialStart中,程序为第一个await停留了2秒,然后又为第二个await停留了1秒。直到第一个计时器结束后,第二个计时器才被创建。
在 concurrentStart中,两个计时器均被创建,然后一起被await。这两个计时器同时运行的,但await的调用仍然是按顺序运行的,这意味着
第二个await会等到第一个await运行结束。这使得代码完成运行只需要2秒,而不是3秒。这2秒是由最慢的计时器决定的。
使用了Promise.all的stillSerial的例子中也存在同样的情况。
如果你希望同时await两个或者是更多的Promise对象,你必须使用Promise.then,正如例子中parallel所做的那样。*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值