The last chained promise does not run

二次信任 提交于 2019-12-11 18:21:25

问题


I have chained a few methods that I'd like to execute in that specific order. The second method deviseLogin() returns a URL that should be passed on the the next method redirect(url)

The first and the second method runs, but I never hear anything from the last redirect(url). Can you see what I'm doing wrong?

  getUserToken = (username, password) ->
    console.log "I'm first"
    $http(
      method: "POST"
      url: "/oauth/token"
      params: params
    ).success((data, status, headers, config) ->
      $scope.$storage.token = data.access_token   


  defer = $q.defer()

  $scope.signIn = (data) ->
    defer.promise
      .then( getUserToken($scope.email, $scope.password) )
      .then( deviseLogin() )
      .then( redirect(url) )


  redirect = (url) ->
    console.log "I'm last"
    console.log "will redirect to #{url}"
    $window.location = data.url

  deviseLogin = () ->
    console.log "I'm second"
    $scope.authErrors = []
    authData = { user: { email: $scope.email, password: $scope.password }}
    $http(
      # Perform a regular Devise login
      method: "POST"
      url: "/users/sign_in"
      data: authData
    )
    .success (data) ->
      # I can see the log lines below and data.url is what I expect it to be
      console.log "logged in devise"
      console.log "data.url: #{data.url}"
      data.url

回答1:


You are calling the functions right away, instead of passing them as callbacks.
The defer.promise part also seems unnecessary.

The code should look like this:

$scope.signIn = (data) ->
    getUserToken(data.email, data.password)
    .then(deviseLogin)
    .then(redirect)



回答2:


I never got the last .then( redirect(url) ) to kick in. Solved the problem by moving redirect(url) to within success() on deviseLogin().

Thanks for all the suggestions and help! :)



来源:https://stackoverflow.com/questions/25060510/the-last-chained-promise-does-not-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!