Javascript 当我需要调用方执行暂停/等待时,如何使用promise.then()?
我把自己和承诺搞混了 我有一个名为Javascript 当我需要调用方执行暂停/等待时,如何使用promise.then()?,javascript,jquery,coffeescript,Javascript,Jquery,Coffeescript,我把自己和承诺搞混了 我有一个名为validateCurrentStep的现有函数,它以多步骤的形式在单击时启动 突然,在其中一个表单元素上的keypress上发生了一些AJAX验证,这意味着在调用validateCurrentStep时验证可能无法完成 validateCurrentStep: -> $step = @getCurrentStep() if valid() return true return false 我想提取承诺列表并暂停此
validateCurrentStep
的现有函数,它以多步骤的形式在单击时启动
突然,在其中一个表单元素上的keypress上发生了一些AJAX验证,这意味着在调用validateCurrentStep
时验证可能无法完成
validateCurrentStep: ->
$step = @getCurrentStep()
if valid()
return true
return false
我想提取承诺列表并暂停此函数,直到返回值
validateCurrentStep: ->
$step = @getCurrentStep()
$promises = $step.data('promises')
# how do I delay wait until the `then` is complete?
$.when($promises).then =>
if valid()
return true
return false
需要多个参数,而不是一系列承诺。在coffeescript中,使用
$.when $promises...
当所有承诺
都成功时,将返回另一个承诺。您不能真正“暂停”函数,但可以在以后调用回调
validateCurrentStep: ->
$step = @getCurrentStep()
$promises = $step.data('promises')
# notice the implicit return values in CS
$.when($promises...).then ->
valid()
validateCurrentStep().then (isValid) ->
# do what you need to do
我真的希望我不需要改变这个函数的调用方式,但听起来这是唯一的方法。谢谢你提醒我多个论点!