From 0c173f58dc909398c29a6c5f41cf89afc6239b6f Mon Sep 17 00:00:00 2001 From: Anton Savoskin Date: Fri, 14 Jun 2019 16:42:26 +0300 Subject: [PATCH] fix(sync): fetch translations in serial order for locize --- src/providers/locize.ts | 4 ++-- src/utils.ts | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/providers/locize.ts b/src/providers/locize.ts index 1dfb6cb..086451d 100644 --- a/src/providers/locize.ts +++ b/src/providers/locize.ts @@ -1,5 +1,5 @@ import { Provider } from './provider'; -import { request, showError, showInfo } from '../utils'; +import { asyncForEach, request, showError, showInfo } from '../utils'; import { Message } from '../types'; type LocizeKeys = { [key: string]: { value: string; context: { text: string } } }; @@ -18,7 +18,7 @@ export class Locize implements Provider { async getKeys(locales: string[]) { const headers = { 'content-type': 'application/json' }; - locales.forEach(async locale => { + asyncForEach(locales, async (locale: string) => { try { this.locizeKeys[locale] = await request({ headers, diff --git a/src/utils.ts b/src/utils.ts index 6e6ebc0..ee3387d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -60,3 +60,8 @@ export const request = ({ url, body, qs, headers = {}, ...rest }: Options) => const format = (time: Date) => time.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, '$1'); export const showError = (message: string) => console.error('\x1b[31m', message, '\x1b[0m'); export const showInfo = (message: string) => console.info(`\x1b[34m[${format(new Date())}]\x1b[0m`, `${message}`); +export const asyncForEach = async (array: any[], callback: any) => { + for (let index = 0; index < array.length; index += 1) { + await callback(array[index], index, array); + } +};