/ walking bear:D / javascript async programming

javascript async programming

2018-11-24 posted in [筆記]

javascript 是函數式、單一綫程、事件驅動、、程式中大量使用 callback 來回應事件通知,callback function 被定義與被執行是在兩個不同的環境和時間點,callback 所引用的外部變數也可能是在兩個不同的狀態,例如:

(function() {
  for (var i = 1; i <= 10; ++i) {
    setTimeout(() => {
      console.log("i=", i);
    }, 100);
  }
})();

(function() {
  for (let i = 1; i <= 10; ++i) {
    setTimeout(() => {
      console.log("i=", i);
    }, 100);
  }
})();

上面的經典案例會因為變數宣告用 var 或 let 而不同?!javascript 也太多令人嚇到吃手手的坑 let 在每一次 iteration 都會創建一個新的 i 變數

async/await 的 mdn 說明

上面的 iframe 是 mdn 利用 CodeMirror 開發的網頁元件,很有趣!值得再花時間研究

這裡再加一個段落的文字 非同步編程系列文promise 這篇寫得還不錯,後篇 async/await 就不是那麼好 . reply_count: 0 get_replies : 0 .

walking bear:DRSS feed

关于

wkliang

Clarke's Three Laws:

  • When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
  • The only way of discovering the limits of the possible is to venture a little way past them into the impossible.
  • Any sufficiently advanced technology is indistinguishable from magic.
  • 版权申明

    知识共享许可协议

    Fork me on GitHub

    Powered by

    Disqus, GitHub, Google Custom Search, Gravatar, HighlightJS, jekyll