上下文2個異步函數:承諾與CoffeeScript的
我需要調用一個函數(函數A)來進行HTTP請求兩個時間,在此之後,我需要調用另一個fonction(函數B),其計算兩個陣列
正常順序應該是
- (功能A &函數A)異步
- 當2-其它完成後,功能B
所以我讓自己承諾,但這裏是問題,我想不出如何正確的語法,我使用Coffeescript。
這是我走到這一步,但現在它不工作
myCoolPromise =() ->
return new Promise (resolve, reject) ->
postRequest(diagUrl, diagnosisBody, storesConnectionObject)
postRequest(storesUrl, brandBody, storesObject)
success = true
if success
resolve 'stuff worked'
else
reject Error 'it broke'
myCoolPromise(storesObject.storesArray, storesConnectionObject.storesArray, absentObject).then (response) ->
console.log 'success', response
handleResult(storesObject.storesArray, storesConnectionObject.storesArray, absentObject)
.catch (error) ->
console.error 'failed', error
我不明白'myCoolPromise'應該做什麼。它似乎不是異步的,「成功」永遠是真的,所以你不應該在這裏使用promise? – Bergi