Skip to content

Commit

Permalink
chore: PromiseUtils refactor generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Pritish Budhiraja committed Jan 17, 2024
1 parent 78382e4 commit 70ad5ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
19 changes: 11 additions & 8 deletions src/screens/HyperSwitch/UserManagement/InviteUsers.res
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,17 @@ let make = () => {
let response = await PromiseUtils.allSettledPolyfill(arrayOfPromises)
if !magicLink {
response->Array.forEachWithIndex((ele, index) => {
if ele !== Js.Json.null {
let passwordFromResponse = ele->getDictFromJsonObject->getString("password", "")
emailPasswordsArray->Array.push(
[
("email", emailList[index]->Option.getWithDefault("")->Js.Json.string),
("password", passwordFromResponse->Js.Json.string),
]->LogicUtils.getJsonFromArrayOfJson,
)
switch Js.Json.classify(ele) {
| Js.Json.JSONObject(jsonDict) => {
let passwordFromResponse = jsonDict->getString("password", "")
emailPasswordsArray->Array.push(
[
("email", emailList[index]->Option.getWithDefault("")->Js.Json.string),
("password", passwordFromResponse->Js.Json.string),
]->LogicUtils.getJsonFromArrayOfJson,
)
}
| _ => ()
}
})
}
Expand Down
14 changes: 10 additions & 4 deletions src/utils/PromiseUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
// Promise.allSettled() takes an iterable of promises and returns a single promise that is fulfilled with an array of promise settlement result

let allSettledPolyfill = (arr: array<promise<Js.Json.t>>) => {
let res = arr->Array.map(promise =>
arr
->Array.map(promise =>
promise
->Promise.then(val => {
Promise.resolve(val)
})
->Promise.catch(_ => {
Js.Json.null->Js.Promise.resolve
->Promise.catch(err => {
switch err {
| Js.Exn.Error(e) =>
let err = Js.Exn.message(e)->Belt.Option.getWithDefault("Failed to Fetch!")
err->Js.Json.string
| _ => Js.Json.null
}->Promise.resolve
})
)
Promise.all(res)
->Promise.all
}

0 comments on commit 70ad5ca

Please sign in to comment.