Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jclem/q-defer
A lightweight promises implementation in Ruby
https://github.com/jclem/q-defer
Last synced: about 1 month ago
JSON representation
A lightweight promises implementation in Ruby
- Host: GitHub
- URL: https://github.com/jclem/q-defer
- Owner: jclem
- License: mit
- Created: 2013-07-08T09:42:42.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-09-29T16:01:36.000Z (about 8 years ago)
- Last Synced: 2024-08-10T09:13:38.183Z (3 months ago)
- Language: Ruby
- Homepage: https://rubygems.org/gems/q-defer
- Size: 4.88 KB
- Stars: 66
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Q
Q is a lightweight promise implementation in Ruby.
**DON'T USE ME!** This project doesn't follow any promises spec. It's all wrong.
## Installation
Add this line to your application's Gemfile:
gem 'q-defer', require: 'q'
And then execute:
$ bundle
Or install it yourself as:
$ gem install q-defer
## Usage
Defer a single block:
```ruby
require "open-uri"Q.defer do |defer|
Thread.new do
response = open("http://www.example.com")if response.status[0] == "200"
defer.resolve(response)
else
defer.reject([response.status, response])
end
end
end.then do |result|
puts "ok"
end.fail do |err, result|
puts "status code: #{err[0]}"
end.always do
puts "All finished!"
end
```Defer multiple blocks:
```ruby
def defer_open(url)
Q.defer do |defer|
Thread.new do
response = open("http://www.example.com")if response.status[0] == "200"
defer.resolve(response)
else
defer.reject([response.status, response])
end
end
end
endQ.when([
defer_open("http://www.example.com"),
defer_open("http://www.google.com")
]).then do |results|
puts "All successful!"
end.fail do |err, results|
puts "Error: #{err}"
end.always do |results|
puts "All successful or one failure."
end
```## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request