Movatterモバイル変換


[0]ホーム

URL:


Te-Chi's Dev Note

Web dev learning notes and reviews. Sharing my discovery of web tech and open source.

© 2017 | Te-Chi's Dev Note | All rights reserved.

Dev Note for Promise

24 Mar 2017promise js 

Abstract

第一次接觸 Promise 的時候是在使用 AngularJS 提供的$http,它是一個基於XMLHttpRequest 的包裝, 回傳的物件是符合Promise/A+ 的 Promise,因此要理解 Promise 的特性與表現, 才能夠正確的實作網頁應用中的非同步操作與使用者介面變化流程。

然而在面對多個具有相依性的非同步操作,程式碼結構會變得複雜難以維護(波動拳式回呼)。 因此,希望藉著這個機會好好檢視MDN 文件 並學習如何撰寫風格良好的 Promise 程式碼。


Promise Behavior Review

beGoodKid.then(beGoodStudent,...).then(workSuccess,...)

Promise Chain Practice

beGoodKid().then(()=>{returnbeGoodStudent()// it's async}).then(()=>{returnworkSuccess()// it's async}).catch()=>{console.warn('your parents are angry')}
constpath=require('path');constos=require('os');constpaths=['file.one','file.two','file.three'];paths.reduce((cur,nextPath)=>{returncur.then(()=>{// return .then() for next promisereturnatom.workspace.open(nextPath);// return an open job as a promise});},Promise.resolve('start'));// init promise

Summary

Related Posts


[8]ページ先頭

©2009-2025 Movatter.jp