ES6 Promises in NodeJS
|Release Date||Apr 23, 2017|
NodeJS SDKs will now return promises in endpoint calls, as an alternative to accepting callbacks.
- We use ES6 Promises which is based on Promises/A+ and is interoperable with most Promise-based implementations.
- Callbacks are still supported (for backward-compatibility) but are now optional.
Re-generate your NodeJS SDK to get the new Promise-enabled SDK implementation now.
Here is an except from a blog by StrongLoop:
All endpoint methods now return Promises.
let promise = apiController.endpointMethod(); promise.then(onFulfilled, onRejected);
onFulfilledmethod is called if the endpoint call succeeds and
onRejectedis called upon error.
onFulfilledmethod will be passed a response from the
endpointMethodif any, after validation.
onRejectedmethod will be passed an error object containing an
errorCodeor a custom error object if defined by your API description.
The sample code above can also be rewritten as:
apiController.endpointMethod() .then(onFulfilled) .catch(onRejected);
Order of callback execution An endpoint call’s callback is always executed before its Promise is resolved.
- Chaining and Nesting Promises
- Promises and synchronous functions
- Error handling in promises
- Doing parallel operations with promises
Have questions? Submit a request.