Project 'ds/pb-services' was moved to 'public-entries/pb-services'. Please update any links and bookmarks that may still have the old path.
Commit 761b95c3 authored by Dmitriy Sintsov's avatar Dmitriy Sintsov 🤗

fix (promise): Add backward compatibility for `Promise.allSettled`

Services:
+ database
+ greeting
parent 42a154d9
......@@ -3,6 +3,14 @@ interface Database {
updateOrSaveIdWithPayload: (id: string, payload: any) => Promise<boolean>
}
async function allSettled (promises: Array<Promise<any>>): Promise<Array<{status: any, value?: any, reason?: any}>> {
const wrappedPromises = promises.map(async (p: Promise<any>) => Promise.resolve(p)
.then(
val => ({ status: 'fulfilled', value: val }),
err => ({ status: 'rejected', reason: err })))
return Promise.all(wrappedPromises)
}
class DatabaseService {
#robotDb!: Database
#remoteDbs: Database[] = []
......@@ -16,7 +24,7 @@ class DatabaseService {
public async save (id: string, payload: any): Promise<boolean> {
const remoteDbs = this.executeAndReturnRemoteDbs(id, payload)
const dbsResults = await Promise.allSettled([...remoteDbs, this.saveInRobotDb(id, payload)])
const dbsResults = await allSettled([...remoteDbs, this.saveInRobotDb(id, payload)])
const resolvedPromises = dbsResults
.filter(promise => promise.status === 'fulfilled')
const isEveryFulfilledDbPromise = resolvedPromises.every(promise => promise)
......
......@@ -24,6 +24,14 @@ class Observer {
}
}
async function allSettled (promises: Array<Promise<any>>): Promise<Array<{ status: any, value?: any, reason?: any }>> {
const wrappedPromises = promises.map(async (p: Promise<any>) => Promise.resolve(p)
.then(
val => ({ status: 'fulfilled', value: val }),
err => ({ status: 'rejected', reason: err })))
return Promise.all(wrappedPromises)
}
interface DataBase {
getUserById?: (id: number) => Promise<any>
updateOrSaveIdWithPayload?: (id: number, payload: any) => Promise<boolean>
......@@ -124,7 +132,7 @@ class GreetingService extends Observer {
}
private async getInfoAboutUserById (id: number): Promise<any> {
const resolvedPromises = await Promise.allSettled(this.getExecutedDataBases(id))
const resolvedPromises = await allSettled(this.getExecutedDataBases(id))
const result = resolvedPromises.reduce((acc: any, res: any) => {
if (res.status === 'fulfilled') {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment