Skip to content

Deferred(Promise/A) implementation and handling HTTP request easily using Deferred

Notifications You must be signed in to change notification settings

waka/objc-Async

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

objc-Async

  • Deferred
  • HttpClient(GET/POST request using Deferred)

Usage

Deferred

// set callback and errback
Deferred *deferred = [Deferred defer];
[deferred then: ^id(id result) {
    // process result object
    return result;
} failure: ^id(id result) {
    NSError *error = (NSError *)result;
    // process error object
}];
[deferred resolve: nil];

// chain callbacks
Deferred *deferred = [Deferred defer];
[[deferred then: ^id(id result) {
    // process result object
    return result;
}] then: ^id(id result) {
    // process result object
    return result;
}];
[deferred resolve: nil];

HttpClient

// get request
NSString *url = @"http://hostname/path/to/api";
NSDictionary *params = @{@"key": @"value"};

Deferred *deferred = [HttpClient doGet: url parameters: params];
[deferred then: ^id(id result) {
    // result is NSDictionary object translated from JSON
    NSDictionary *data = (NSDictionary *)result;
    // process data
    return data;
}];
[deferred resolve: nil];

// post request
NSString *url = @"http://hostname/path/to/api";
NSDictionary *params = @{@"key": @"value"};

Deferred *deferred = [HttpClient doPost: url parameters: params];
[deferred then: ^id(id result) {
    // result is NSDictionary object that translated from JSON
    NSDictionary *data = (NSDictionary *)result;
    // process data
    return data;
} failure: ^id(id result) {
    // result is NSError object
    NSError *error = (NSError *)result;
    // process error
    return error;
}];
[deferred resolve: nil];

About

Deferred(Promise/A) implementation and handling HTTP request easily using Deferred

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published