使用角度$ q和条纹避免厄运金字塔

时间:2016-07-23 05:46:10

标签: angularjs promise stripe-payments chaining

我正在尝试使用角度Traceback (most recent call last): File "/home/mahmood/PycharmProjects/sentence2vec/graphClustering.py", line 22, in <module> y = cl.fit_predict(matrix) File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.17.1-py2.7-linux-x86_64.egg/sklearn/base.py", line 371, in fit_predict self.fit(X) File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.17.1-py2.7-linux-x86_64.egg/sklearn/cluster/spectral.py", line 454, in fit assign_labels=self.assign_labels) File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.17.1-py2.7-linux-x86_64.egg/sklearn/cluster/spectral.py", line 258, in spectral_clustering eigen_tol=eigen_tol, drop_first=False) File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.17.1-py2.7-linux-x86_64.egg/sklearn/manifold/spectral_embedding_.py", line 254, in spectral_embedding tol=eigen_tol) File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/eigen/arpack/arpack.py", line 1507, in eigsh raise ValueError("k must be between 1 and the order of the " ValueError: k must be between 1 and the order of the square input matrix. 的实现来做一些承诺链,但似乎已经遇到了障碍,几乎被迫陷入金字塔的厄运状态。

我更喜欢做什么:

$q

请注意function doYetAnotherAsync() { return $q(function(resolve, reject){ var transaction = { cvc: vm.cvc, exp_year: vm.expirationYear, exp_month: vm.expirationMonth, number: vm.cardNumber } Stripe.card.createToken(transaction, function(responseCode, response) { console.log('createToken'); if(responseCode === 200) { resolve({ id: core.get(response, 'id'), cardId: core.get(response, 'card.id') }); } reject(response); }); }); } function doAnotherAsync() { return $q(function(resolve, reject){ var globalVar; doSomeNormalAjaxCall() .then(function(response){ globalVar = response.id; return doYetAnotherAsync() }) .then(function(stripeData){ console.log('stripeData', stripeData); // stripeData returns a $$state object return finalAsync() }) .then(function(response){ // handle final data here // resolve/reject $q }) }) } doSomeAsync() .then(doAnotherAsync) ,我希望能够使用此表单:

doAnotherAsync()

避免金字塔的厄运。

但是,在上面的代码中,第二个someAsync() .then(function(){ return someOtherAsync() }) .then(function(){ return yetAnotherAsync() }) .then(function(){ // finally do some stuff }) 提前解析,.then()对象沿着链传递,而不是$$state的普通对象。

{id: '', cardId: ''}订单示例:

console.log

为了保留承诺链接顺序并获得真实的对象响应,我被迫做了:

stripeData d {$$state: Object}
XHR finished loading: POST "https://api.stripe.com/v1/tokens"
createToken

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

不要过分担心金字塔。

金字塔并不总是可以避免,并且可以允许:

  • 通过关闭访问以前的结果
  • 捕捉特定错误

通过利用金字塔中的闭包,您通常会避免使用外部var(您的globalVar),因此在工作版本中,金字塔和外部变量都令人惊讶。

由于代码已经简化到特定问题不公开的程度,因此很难进一步提出建议。 globalVar的需求尚不清楚,金字塔看起来应该成功展平。也许在some logic区块中发生了某些事情,或者return中可能缺少finalyAsync()

相关问题