Skip to content

Instantly share code, notes, and snippets.

@DmitryMasley
Last active March 22, 2018 12:45
Show Gist options
  • Save DmitryMasley/d20aabb2054f7142666c630ceed92c29 to your computer and use it in GitHub Desktop.
Save DmitryMasley/d20aabb2054f7142666c630ceed92c29 to your computer and use it in GitHub Desktop.
// returns collection with identifer `id`
const getStrings = () => {
return new Promise((resolve) => {
resolve([
{
id: "id",
sourceString: "{1} word"
}
])
});
}
// accepts an array of ids, returns collection
const getPlaceholders = (stringIds) => {
return new Promise((resolve) => {
resolve(stringIds.map(id => ({id, placeholders: []})))
});
}
// accepts array of ids, returns collection
const getKeys = (stringIds) => {
return new Promise((resolve) => {
resolve(stringIds.map(id => ({id, key: "key"})))
});
}
// returns { strings, placeholders, keys }
// get strings first, than using their ids make requests to keys and placeholders (parallely of course)
// return all three collections in one object
// if getKeys gets rejected still return resolved promise with { strings, placeholders, keys: [] }
const getStringData = () => {
return new Promise((resolve) => {
getStrings().then((strings) => {
resolve(strings);
})
})
.then((strings) => {
return getPlaceholders(strings)
.then(getKeys(strings))
})
.then((strings, placeholders, keys) => {
return Promise.resolve({ strings, placeholders, keys });
})
}
// task is to implement getStringData as described above, current implementaion is not correct
// can be done with Promises or async/await syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment