亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解ES6 Promise對(duì)象then方法鏈?zhǔn)秸{(diào)用

 更新時(shí)間:2018年10月20日 14:22:40   作者:不知不問(wèn)  
這篇文章主要介紹了詳解ES6 Promise對(duì)象then方法鏈?zhǔn)秸{(diào)用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

promise俗稱(chēng)鏈?zhǔn)秸{(diào)用,它是es6中最重要的特性之一

簡(jiǎn)單的說(shuō)可以不停的then調(diào)用嵌套在調(diào)用(異步之后,鏈?zhǔn)秸{(diào)用方式執(zhí)行回調(diào)),這種操作方式稱(chēng)為promise

then()方法的作用是Promise實(shí)例添加解決(fulfillment)和拒絕(rejection)狀態(tài)的回調(diào)函數(shù)。then()方法會(huì)返回一個(gè)新的Promise實(shí)例,所以then()方法后面可以繼續(xù)跟另一個(gè)then()方法進(jìn)行鏈?zhǔn)秸{(diào)用。

let p = new Promise((resolve, reject) => {
  setTimeout(resolve, 1000, 'success');
});
p.then(
  res => {
    console.log(res);
    return `${res} again`;
  }
)
  .then(
    res => console.log(res)
  );
// 連續(xù)
// success
// success again

但是前一個(gè)then()方法中的回調(diào)函數(shù)中又可能返回一個(gè)Promise實(shí)例,這時(shí)候后面一個(gè)then()方法中的回調(diào)函數(shù)會(huì)等前一個(gè)Promise實(shí)例的狀態(tài)發(fā)生變化才會(huì)調(diào)用。

let p = new Promise((resolve, reject) => {
  setTimeout(resolve, 1000, 'success');
});
p.then(
  res => {
    console.log(res);
    return new Promise((resolve, reject) => {
      setTimeout(resolve, 1000, 'success');
    });
  }
)
  .then(
    res => console.log(res)
  );
// 相隔1000ms
// success
// success

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論