跳到內容 跳到搜尋
方法
P
T
V

實例公開方法

pending?()

傳回關聯查詢是否仍正在執行。

# File activerecord/lib/active_record/promise.rb, line 13
def pending?
  @future_result.pending?
end

then(&block)

傳回新的 ActiveRecord::Promise,會在存取值時套用傳入的區塊

Post.async_pick(:title).then { |title| title.upcase }.value
# => "POST TITLE"
# File activerecord/lib/active_record/promise.rb, line 36
def then(&block)
  Promise.new(@future_result, @block ? @block >> block : block)
end

value()

傳回查詢結果。如果查詢尚未完成,存取 #value 會阻擋,直到查詢完成。如果查詢失敗,#value 會引發對應的錯誤。

# File activerecord/lib/active_record/promise.rb, line 20
def value
  return @value if defined? @value

  result = @future_result.result
  @value = if @block
    @block.call(result)
  else
    result
  end
end