Title: Support Promises for API calls · Issue #90 · PlayFab/JavaScriptSDK · GitHub
Open Graph Title: Support Promises for API calls · Issue #90 · PlayFab/JavaScriptSDK
X Title: Support Promises for API calls · Issue #90 · PlayFab/JavaScriptSDK
Description: I've been using both this SDK and the NodeJS SDK extensively for a few months now, and I've found that the SDKs are much easier to use when API calls are promise-based. I've been using a wrapper function that provides this functionality,...
Open Graph Description: I've been using both this SDK and the NodeJS SDK extensively for a few months now, and I've found that the SDKs are much easier to use when API calls are promise-based. I've been using a wrapper fu...
X Description: I've been using both this SDK and the NodeJS SDK extensively for a few months now, and I've found that the SDKs are much easier to use when API calls are promise-based. I've been using ...
Opengraph URL: https://github.com/PlayFab/JavaScriptSDK/issues/90
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support Promises for API calls","articleBody":"I've been using both this SDK and the NodeJS SDK extensively for a few months now, and I've found that the SDKs are much easier to use when API calls are promise-based. I've been using a wrapper function that provides this functionality, which works fine but is not ideal. That looks something like this (shortened for brevity) \r\n\r\n```javascript\r\nfunction playfabAPI(endpoint, requestObject) {\r\n return new Promise(function(resolve, reject) {\r\n PlayFabClientSDK[endpoint](requestObject, function(error, result) {\r\n if (error !== null) {\r\n // Request error\r\n return reject(new Error(error));\r\n }\r\n else if (result \u0026\u0026 result[\"code\"] == 200) {\r\n // Request successful\r\n return resolve(result);\r\n }\r\n else {\r\n // Non-200 HTTP status\r\n return reject(new Error(result.status));\r\n }\r\n });\r\n });\r\n}\r\n``` \r\n\r\nThis makes for cleaner code, adds useful functionality, and eliminates a lot of redundant error checking code. The current request functions are consistent enough for this to work in most cases so far but I'm a bit wary of using it in production. What I would propose is to implement this either alongside the current format, or as an additional SDK/script, for compatibility reasons. \r\n\r\n\u003chr\u003e \r\n\r\n### Example of how this change could be implemented in a given function: \r\n\r\n#### Current\r\n```javascript\r\nexports.UpdateUserData = function (request, callback) {\r\n if (PlayFab._internalSettings.sessionTicket == null) {\r\n throw \"Must be logged in to call this method\";\r\n }\r\n PlayFab.MakeRequest(\r\n PlayFab.GetServerUrl() + \"/Client/UpdateUserData\",\r\n request,\r\n \"X-Authorization\",\r\n PlayFab._internalSettings.sessionTicket,\r\n function (error, result) {\r\n if (callback != null) {\r\n callback(error, result);\r\n }\r\n },\r\n );\r\n};\r\n```\r\n\r\n#### Proposed\r\n```javascript\r\nexports.UpdateUserData = function (request)\r\n{\r\n return new Promise((resolve, reject) =\u003e {\r\n if (PlayFab._internalSettings.sessionTicket == null) {\r\n throw \"Must be logged in to call this method\";\r\n }\r\n PlayFab.MakeRequest(\r\n PlayFab.GetServerUrl() + \"/Client/UpdateUserData\",\r\n request,\r\n \"X-Authorization\",\r\n PlayFab._internalSettings.sessionTicket,\r\n (error, result) =\u003e {\r\n if (error !== null) {\r\n return reject(new Error(error));\r\n }\r\n else if (result \u0026\u0026 result[\"code\"] == 200) {\r\n return resolve(result);\r\n }\r\n else {\r\n return reject(new Error(result.status));\r\n }\r\n },\r\n );\r\n });\r\n};\r\n``` \r\n\r\n\u003chr\u003e \r\n\r\n### Example usage of current vs proposed usage: \r\n#### Current\r\n```javascript\r\nPlayFabClientSDK.UpdateUserData(requestParameters, function(error, result) {\r\n // Check for + handle errors... \r\n // Handle result...\r\n});\r\n```\r\n#### Proposed\r\n```javascript\r\nPlayFabClientSDK.UpdateUserData(requestParameters).then((userDataResult) =\u003e {\r\n // Handle userDataResult...\r\n}).catch((error) =\u003e {\r\n // Handle errors... \r\n});\r\n```\r\n\r\nThis would also have the added benefit of allowing the use of async/await which can greatly simplify code structure, e.g. when chaining requests\r\n```javascript\r\n(async function() {\r\n const userDataResult = await PlayFabClientSDK.UpdateUserData(requestParameters);\r\n // Handle userDataResult/make additional API requests\r\n})();\r\n```\r\n\r\n\u003chr\u003e\r\n\r\nLet me know what you guys think. Is this something that would be better served using the SDK generator with a custom config, or would it be feasible to get an official implementation? ","author":{"url":"https://github.com/403-Fruit","@type":"Person","name":"403-Fruit"},"datePublished":"2020-12-02T23:18:10.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/90/JavaScriptSDK/issues/90"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:568ab3c5-7dcf-1d56-ab72-4ca7e8d8c2a6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 877E:3F65C8:FF99FF:1526B89:69717AED |
| html-safe-nonce | 086a53fdd62e12a77ea2775a234989433cddc050236ffd82743ada28cc782f32 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NzdFOjNGNjVDODpGRjk5RkY6MTUyNkI4OTo2OTcxN0FFRCIsInZpc2l0b3JfaWQiOiI2ODM3NTUzNTM0MzEzMDAzNzU3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3cf45d7fc1bfffe191970b360e0dd2c9e97387111d04fed14fc85d024d2e58ad |
| hovercard-subject-tag | issue:755683000 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PlayFab/JavaScriptSDK/90/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1f0984698becf34038ebcfb3621ec858cf17f802629bc427dd144a32a139b689/PlayFab/JavaScriptSDK/issues/90 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1f0984698becf34038ebcfb3621ec858cf17f802629bc427dd144a32a139b689/PlayFab/JavaScriptSDK/issues/90 |
| og:image:alt | I've been using both this SDK and the NodeJS SDK extensively for a few months now, and I've found that the SDKs are much easier to use when API calls are promise-based. I've been using a wrapper fu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | 403-Fruit |
| hostname | github.com |
| expected-hostname | github.com |
| None | 102ab681b33bed6a87d35dc54dd8200470d7e01648f08979e84415ab67d611aa |
| turbo-cache-control | no-preview |
| go-import | github.com/PlayFab/JavaScriptSDK git https://github.com/PlayFab/JavaScriptSDK.git |
| octolytics-dimension-user_id | 7634333 |
| octolytics-dimension-user_login | PlayFab |
| octolytics-dimension-repository_id | 31617208 |
| octolytics-dimension-repository_nwo | PlayFab/JavaScriptSDK |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31617208 |
| octolytics-dimension-repository_network_root_nwo | PlayFab/JavaScriptSDK |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 6a660b76f271bda19afb9f5242b473bfee637a5a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width