Unity Rest Client

Callbacks VS Promises

Coroutines

are computer-program components that generalize subroutines for non-preemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations.

Singleton Pattern

is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

Callbacks

RequestHelper.GetArray<Post>(root + "/posts", (err, res) => {
    if(err != null){
        EditorUtility.DisplayDialog ("Error", err.Message, "Ok");
    }
    else{
	EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<Post>(res, true), "Ok");
    }

    RequestHelper.GetArray<Todo>(root + "/todos", (err, res) => {
        if(err != null){
            EditorUtility.DisplayDialog ("Error", err.Message, "Ok");
        }
        else{
    	    EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<Todo>(res, true), "Ok");
        }
        //RequestHelper.GetArray<User>(root + "/users"...
    });
});

Promises

RequestHelper.GetArray<Post>(root + "/posts").Then(res => {
    EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<Post>(res, true), "Ok");

    return RequestHelper.GetArray<Todo>(root + "/todos");
}).Then(res => {
    EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<Todo>(res, true), "Ok");

    return RequestHelper.GetArray<User>(root + "/users");
}).Then(res => {
    EditorUtility.DisplayDialog ("Success", JsonHelper.ArrayToJson<User>(res, true), "Ok");
}).Catch(err => EditorUtility.DisplayDialog ("Error", err.Message, "Ok"));

Demo

Referencias

Unity Rest Client

By J.D Nicholls

Unity Rest Client

  • 1,950