[New Library] Particle Promise

New library, ParticlePromise creates Javascript-like Promises (API calls w/ callbacks) for your Particle devices.
It allows you to write a statement like

promise.create(myWebhookCaller, "MyResponseTopic")
       .then(myCallbackFunction)
       .timeout(myTimeoutFunction);

and forget about managing the rest. ParticlePromise will call the appropriate functions at the appropriate time. You can also create functions for errors, (.error(myErrorFunction) or .then(myCallbackFunction, myErrorFunction)), and final functions (.finally(myFinalFunction)) that are always run no matter the outcome.

ParticlePromise can also accept inline(lambda) functions so that all of your API calling code can be co-located in one place.

promise.create([]{
          Particle.publish("WebhookName", NULL, 60, PRIVATE);
        }, "MyResponseTopic")
       .then([](const char* event, const char* data){
          Serial.printlnf("My data is: %s", data);
        });

Available on Particle Build and through CLI via particle library add ParticlePromise

The syntax is a bit different than some C++ and Arduino/Particle/Wiring coders are used to, so there’s a whole suite of examples available to guide users in getting started in addition to a Readme.

I appreciate any and all feedback on where and how this library can be improved.

4 Likes

This looks really cool. I look forward to playing with it. Thank you for posting.

Cheers,
Tom