All Projects → BlackMATov → Promise.hpp

BlackMATov / Promise.hpp

Licence: mit
C++ asynchronous promises like a Promises/A+

Programming Languages

cpp
1120 projects
cpp17
186 projects

Projects that are alternatives of or similar to Promise.hpp

alls
Just another library with the sole purpose of waiting till all promises to complete. Nothing more, Nothing less.
Stars: ✭ 13 (-58.06%)
Mutual labels:  promises, promise
combine-promises
Like Promise.all(array) but with an object instead of an array.
Stars: ✭ 181 (+483.87%)
Mutual labels:  promises, promise
Fetch
Asynchronous HTTP client with promises.
Stars: ✭ 29 (-6.45%)
Mutual labels:  promises, promise
promiviz
Visualize JavaScript Promises on the browser. Visualize the JavaScript Promise APIs and learn. It is a playground to learn about promises faster, ever!
Stars: ✭ 79 (+154.84%)
Mutual labels:  promises, promise
Yaku
A lightweight promise library
Stars: ✭ 276 (+790.32%)
Mutual labels:  promise, promises
bluff
🙏 Promise A+ implementation
Stars: ✭ 14 (-54.84%)
Mutual labels:  promises, promise
ProtoPromise
Robust and efficient library for management of asynchronous operations in C#/.Net.
Stars: ✭ 20 (-35.48%)
Mutual labels:  promises, promise
Functional Promises
Write code like a story w/ a powerful Fluent (function chaining) API
Stars: ✭ 141 (+354.84%)
Mutual labels:  promise, promises
tall
Promise-based, No-dependency URL unshortner (expander) module for Node.js
Stars: ✭ 56 (+80.65%)
Mutual labels:  promises, promise
doasync
Promisify functions and objects immutably
Stars: ✭ 27 (-12.9%)
Mutual labels:  promises, promise
Q
A platform-independent promise library for C++, implementing asynchronous continuations.
Stars: ✭ 179 (+477.42%)
Mutual labels:  promise, promises
Asyncro
⛵️ Beautiful Array utilities for ESnext async/await ~
Stars: ✭ 487 (+1470.97%)
Mutual labels:  promise, promises
Promise
Promise / Future library for Go
Stars: ✭ 149 (+380.65%)
Mutual labels:  promise, promises
market-pricing
Wrapper for the unofficial Steam Market Pricing API
Stars: ✭ 21 (-32.26%)
Mutual labels:  promises, promise
Tascalate Concurrent
Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s
Stars: ✭ 144 (+364.52%)
Mutual labels:  promise, promises
redux-reducer-async
Create redux reducers for async behaviors of multiple actions.
Stars: ✭ 14 (-54.84%)
Mutual labels:  promises, promise
Breeze
Javascript async flow control manager
Stars: ✭ 38 (+22.58%)
Mutual labels:  promise, promises
Vine
Python promises
Stars: ✭ 83 (+167.74%)
Mutual labels:  promise, promises
swear
🙏 Flexible promise handling with Javascript
Stars: ✭ 56 (+80.65%)
Mutual labels:  promises, promise
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+12090.32%)
Mutual labels:  promise, promises

promise.hpp

travis appveyor codecov language license paypal

Installation

promise.hpp is a header-only library. All you need to do is copy the headers files from headers directory into your project and include them:

#include "promise.hpp/promise.hpp"

Also, you can add the root repository directory to your cmake project:

add_subdirectory(external/promise.hpp)
target_link_libraries(your_project_target promise.hpp)

Examples

Creating a promise

#include "promise.hpp"
using promise_hpp;

promise<std::string> download(const std::string& url)
{
    promise<std::string> result;

    web_client.download_with_callbacks(
        [result](const std::string& html) mutable
        {
            result.resolve(html);
        },
        [result](int error_code) mutable
        {
            result.reject(std::runtime_error("error code: " + std::to_string(error_code)));
        });

    return result;
}

Alternative way to create a promise

promise<std::string> p = make_promise<std::string>(
    [](auto&& resolver, auto&& rejector)
    {
        web_client.download_with_callbacks(
            [resolver](const std::string& html) mutable
            {
                resolver(html);
            },
            [rejector](int error_code) mutable
            {
                rejector(std::runtime_error("error code: " + std::to_string(error_code)));
            });
    });

Waiting for an async operation

download("http://www.google.com")
    .then([](const std::string& html)
    {
        std::cout << html << std::endl;
    })
    .except([](std::exception_ptr e)
    {
        try {
            std::rethrow_exception(e);
        } catch (const std::exception& ee) {
            std::cerr << ee.what() << std::endl;
        }
    });

Chaining async operations

download("http://www.google.com")
    .then([](const std::string& html)
    {
        return download(extract_first_link(html));
    })
    .then([](const std::string& first_link_html)
    {
        std::cout << first_link_html << std::endl;
    })
    .except([](std::exception_ptr e)
    {
        try {
            std::rethrow_exception(e);
        } catch (const std::exception& ee) {
            std::cerr << ee.what() << std::endl;
        }
    });

Transforming promise results

download("http://www.google.com")
    .then([](const std::string& html)
    {
        return extract_all_links(html);
    })
    .then([](const std::vector<std::string>& links)
    {
        for ( const std::string& link : links ) {
            std::cout << link << std::endl;
        }
    });

Combining multiple async operations

std::vector<std::string> urls{
    "http://www.google.com",
    "http://www.yandex.ru"};

std::vector<promise<std::string>> url_promises;
std::transform(urls.begin(), urls.end(), url_promises.begin(), download);

make_all_promise(url_promises)
    .then([](const std::vector<std::string>& pages)
    {
        std::vector<std::string> all_links;
        for ( const std::string& page : pages ) {
            std::vector<std::string> page_links = extract_all_links(page);
            all_links.insert(
                all_links.end(),
                std::make_move_iterator(page_links.begin()),
                std::make_move_iterator(page_links.end()));
        }
        return all_links;
    })
    .then([](const std::vector<std::string>& all_links)
    {
        for ( const std::string& link : all_links ) {
            std::cout << link << std::endl;
        }
    });

Chaining multiple async operations

download("http://www.google.com")
    .then_all([](const std::string& html)
    {
        std::vector<promise<std::string>> url_promises;
        std::vector<std::string> links = extract_all_links(html);
        std::transform(links.begin(), links.end(), url_promises.begin(), download);
        return url_promises;
    })
    .then([](const std::vector<std::string>& all_link_page_htmls)
    {
        // ...
    })
    .except([](std::exception_ptr e)
    {
        // ...
    });

API

coming soon!

License (MIT)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].