Пример #1
0
    void Core<T>::resolve(const Promise<T>& promise) {
	if (_status != PENDING)
	    throw std::runtime_error("The promise is already resolve or reject");

	Promise<T> p = promise;

	Deferred<T> d(lock());
	p.then([d] (const T& value) mutable {
		d.resolve(value);
	    });
	p.otherwise([d] (const std::string& err) mutable {
		d.reject(err);
	    });
    }
Пример #2
0
int main()
{
    Deferred<int> defer = deferred<int>();
    Promise<int> p = defer.promise();

    Promise<std::string> p2 = p.then ([] (int a) -> std::string {
	    std::cout << "Defered was resolved with [" << a << "]" << std::endl;
	    return "Success";
	});

    p.otherwise([] (const std::string& error) -> void {
	    std::cout << "Defer was rejected with [" << error << "]" << std::endl;
	});


    auto p3 = p2.then([] (const std::string& result) -> void {
	    std::cout << "2nd Defer was resolved with [" << result << "]" << std::endl;
	});

    p2.otherwise([] ( const std::string& err) {
	    std::cout << "2nd Defer was rejected with [" << err << "]" << std::endl;
	});


    p3.then ([] (const bool& b) { 
	    std::cout << "3rd defer resolved with [" << b << "]" << std::endl;
	});


    //
    defer.resolve(42);
    //
    // OR
    //
    // defer.reject("There was an error");
    //
}