I have a dict like this:
{go: ['went', 'run'], love: ['passion', 'like']}
The value of a key is its synonyms. And 'getSynonymWords(word)' is a async function that returns a promise in which Its value is a list of synonym words corresponding with the parameter passed. How can I loop through the object to get another object recursively like this:
{went: [], run: [], passion: [], like: []}
This is my piece of code:
function getRelatedWords(dict) {
return new Promise(function(resolve) {
var newDict = {};
for(var key in dict){
if (dict.hasOwnProperty(key)) {
var synonyms = dict[key];
Promise.map(synonyms, function (synonym) {
return getSynonymWords(synonym).then(function (synonyms) {
newDict[synonym] = synonyms;
return newDict;
});
}).then(function () {
resolve(newDict);
});
}
}
});
}
It is incorrect because some tasks are not finished, But I don't know how to run tasks parallel nested with promises. I'm using Bluebird library. Could you help me?