diff --git a/CHANGELOG.md b/CHANGELOG.md index c895993ef6..6968686dd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ The following is a curated list of changes in the Enact project, newest changes on the top. +## [unreleased] + +### Added + +- `webos/LS2Request.sendLS2Request` to provide LS2Request.send method with Promise + ## [4.9.0-beta.1] - 2024-06-17 No significant changes. diff --git a/packages/webos/LS2Request/LS2Request.js b/packages/webos/LS2Request/LS2Request.js index 5d2aa96664..f0758f5733 100644 --- a/packages/webos/LS2Request/LS2Request.js +++ b/packages/webos/LS2Request/LS2Request.js @@ -183,3 +183,53 @@ export default class LS2Request { } } } + +/** + * Provides a Promise which calls LS2Request.send to give onSuccess and onFailure as callback functions of Promise methods. + * + * Usage: + * ``` + * sendLS2Request(options).then(onSuccess, onFailure); + * ``` + * + * @function + * @memberof webos/LS2Request + * @param {Object} options Options for the LS2 Request call except onSuccess and onFailure. + * @param {String} options.service The name of the LS2 service. + * @param {String} options.method The name of the method. + * @param {Object} options.parameters Any parameters required by the service method. + * @param {Function} options.onComplete The handler to run when the request + * is completed, regardless of return status. + * @param {Function} options.onTimeout The handler to run when the request + * times out. Used in conjunction with `timeout`. + * @param {Boolean} options.subscribe Subscribe to service methods that support subscription. + * @param {Number} options.timeout The delay in milliseconds to wait for the request to return. + * @returns {Promise} Promise which runs LS2Request.send + * @public + */ +export const sendLS2Request = ({ + service = '', + method = '', + parameters = {}, + onComplete = null, + onTimeout = timeoutHandler, + subscribe = false, + timeout = 0 +}) => { + return new Promise((resolve, reject) => { + new LS2Request().send({ + service, + method, + parameters, + onSuccess: resolve, + onFailure: result => { + failureHandler(result); + reject(result); + }, + onComplete, + onTimeout, + subscribe, + timeout + }); + }); +};