(Node) Dynamic promises that run n-times - javascript

I have an array of objects containing a users info.
var names=[{name: 'yoda',
address:'123 Compton',
email:'yodalicious#force.com'},
{name: 'darth vader',
address:'69 harlem',
email:'elevader#force.com'},{....}]
this is a list that can range from 100 to 1000 users.
The problem is that I need to request/pull information from the web and DB about each user and create a new array containing more details about each user. this will be 3 sources. Now each pull takes anywhere from 10 ms to 1 min.
that is where I jump to promises. ("native-promise-only"), as an example:
require("native-promise-only");
function getFile(file) {
return new Promise(function(resolve){
fakeAjax(file,resolve);
});
}
//Recursive call
function recursivePromise(originalArray, newArray){
if(isEmpty(originalArray) ){
store(newArray);
display(newArray);
}
else{
var currentItem = originalArray[0];
var p1 = getFile(currentItem.name);
var p2 = getFile(currentItem.address );
var p3 = getFile(currentItem.email);
var newDataItem={};
p1.then(function(msg){
//TODO check status
newDataItem.nameinfo= msg;
return p2;
})
.then(function(msg){
//TODO check status
newDataItem.addressinfo= msg
return p3;
})
.then(function(msg){
newDataItem.emailinfo= msg
newArray.add(newDataItem);
recursivePromise(originalArray.shift(), newArray)
});
}
}
var new_array=[];
recursivePromise(names, new_array);
this is a rough code, I have something similar and it works! somewhat. But a bug in me tells me that i might be setting up a future failure. I am doing it recursively because the position of the item in 'names' is important. so they need to be processed in order.

Issues as I see them :
Coding errors :
fruits comes out of nowhere; presumably originalArray.shift() was intended.
array.shift() removes the 0th element from the array and returns the removed element, not the array.
originalArray would be destroyed, which is not a good idea.
.add() is not an array method, presumably .push() was intended.
Pattern issues :
The p1.then(...).then(...) chain is a rather cumbersome way to build newDataItem; Promise.all(p1, p2, p3).then(...) would be more readable, with newDataItem built in a single expression.
Recursion is not necessary; the process would be more simply coded as ...; Promise.all(arrayOfPromises).then(function(newArray) {...}); (that's a second use of Promise.all()); although the promises will settle in any order they like, newArray will be congruent with originalArray.
newArray can be generated internally, without needing to pass in [].
function process(originalArray) {
var promises = originalArray.map(function(currentItem) {
var p1 = getFile(currentItem.name);
var p2 = getFile(currentItem.address);
var p3 = getFile(currentItem.email);
return Promise.all([p1, p2, p3]).then(function(results) {
return {
nameinfo: results[0],
addressinfo: results[1],
emailinfo: results[2]
};
});
});
return Promise.all(promises).then(function(newArray) {
display(newArray);
return newArray;
});
}
process(names).then(function(newArray) {
store(newArray);
});
display(newArray) and store(newArray) could both be executed internally or externally. I split them above to demonstrate the two possibilities.

Related

How can I run a callback after a loop of Promises?

Specifically, given a list of data, I want to loop over that list and do a fetch for each element of that data before I combine it all afterward. The thing is, as written, the code iterates through the entire list immediately, starting all the operations at once. Then, even though the fetch operations are still running, the then call I have after all that runs, before the data could've been processed.
I read something about putting all the Promises in an array, then passing that array to a Promise.all() call, followed by a then that will have access to all that processed data as intended, but I'm not sure how exactly to go about doing it in this case, since I have nested Promises in this for loop.
for(var i in repoData) {
var repoName = repoData[i].name;
var repoUrl = repoData[i].url;
(function(name, url) {
Promise.all([fetch(`https://api.github.com/repos/${username}/${repoData[i].name}/commits`),
fetch(`https://api.github.com/repos/${username}/${repoData[i].name}/pulls`)])
.then(function(results) {
Promise.all([results[0].json(), results[1].json()])
.then(function(json) {
//console.log(json[0]);
var commits = json[0];
var pulls = json[1];
var repo = {};
repo.name = name;
repo.url = url;
repo.commitCount = commits.length;
repo.pullRequestCount = pulls.length;
console.log(repo);
user.repositories.push(repo);
});
});
})(repoName, repoUrl);
}
}).then(function() {
var payload = new Object();
payload.user = user;
//console.log(payload);
//console.log(repoData[0]);
res.send(payload);
});
Generally when you need to run asynchronous operations for all of the items in an array, the answer is to use Promise.all(arr.map(...)) and this case appears to be no exception.
Also remember that you need to return values in your then callbacks in order to pass values on to the next then (or to the Promise.all aggregating everything).
When faced with a complex situation, it helps to break it down into smaller pieces. In this case, you can isolate the code to query data for a single repo into its own function. Once you've done that, the code to query data for all of them boils down to:
Promise.all(repoData.map(function (repoItem) {
return getDataForRepo(username, repoItem);
}))
Please try the following:
// function to query details for a single repo
function getDataForRepo(username, repoInfo) {
return Promise
.all([
fetch(`https://api.github.com/repos/${username}/${repoInfo.name}/commits`),
fetch(`https://api.github.com/repos/${username}/${repoInfo.name}/pulls`)
])
.then(function (results) {
return Promise.all([results[0].json(), results[1].json()])
})
.then(function (json) {
var commits = json[0];
var pulls = json[1];
var repo = {
name: repoInfo.name,
url: repoInfo.url,
commitCount: commits.length,
pullRequestCount: pulls.length
};
console.log(repo);
return repo;
});
}
Promise.all(repoData.map(function (repoItem) {
return getDataForRepo(username, repoItem);
})).then(function (retrievedRepoData) {
console.log(retrievedRepoData);
var payload = new Object();
payload.user = user;
//console.log(payload);
//console.log(repoData[0]);
res.send(payload);
});

Attempt to use Promises to delete records

Here is a simple task I would like to accomplish on Parse.com with Cloud Code.
The task consists to delete a Unit and what is related to it.
One Unit has several Sentences related to it and each Sentence has one or more Translations.
So when the task is performed, the Unit as well as the Sentence and Translations should be deleted.
I have a strong feeling I should be using Promises (and chain them up) in order to do what I want in a good manner.
Below is the code I wrote, but it works only partially (The translations are deleted, not the rest).
Parse.Cloud.define("deleteUnitAndDependencies", function(request, response) {
var unitListQuery = new Parse.Query("UnitList");
unitListQuery.equalTo("objectId", request.params.unitID);
unitListQuery.equalTo("ownerID", request.params.userID);
unitListQuery.find().then(function(resUnit) {
var sentenceListQuery = new Parse.Query("SentenceList");
sentenceListQuery.equalTo("unit", resUnit[0]);
return sentenceListQuery.find();
}).then(function(resSentence) {
var translatListQuery = new Parse.Query("TranslatList");
for (i = 0; i < resSentence.length; i++) {
var query = new Parse.Query("TranslatList");
query.equalTo("sentence", resSentence[i]);
translatListQuery = Parse.Query.or(translatListQuery, query);
}
return translatListQuery.find();
}).then(function(resTranslat) {
for (iT = 0; iT < resTranslat.length; iT++) {
resTranslat[iT].destroy({});
}
});
});
I surely need to add some lines of code like:
resSentence[x].destroy({});
and:
resUnit[0].destroy({});
The problem is that I do not quite see where is the adequate place for that.
Collect the objects to be deleted then use Parse.Object.destroyAll(someArray); to delete all at once.
In cases like this I like to use a scope variable to hold things for later use.
var scope = {
sentences: [],
units: []
};
// later inside then block...
scope.sentences.push(resSentence[i]);
// ...now we have them collected safely
.then(function() {
return Parse.Object.destroyAll(scope.sentences);
})

RSVP.js - Multiple asynchronous function calls on array

I have a result from a REST call that contains a list of files. Each file has properties that I have to extract and place in a new array. This is straightforward and is easily done with a simple loop. Three of the properties I need to extract are accessible in a direct way, while three other properties are of HATEOAS type, which in other words mean that for each file in the result, I have to make three other asynchronous calls to retrieve it's values.
My first instinct was to use RSVP.all() to process my promises and to map the items in the new array to the corresponding properties in the original list of files using map, but I cannot figure out how to achieve this.
I want to achieve something like below, but I have no idea how I can get the index of the current mapped item in itemList, to include the correct file from fileList. How can I do this?
On a sidenote, if I use RSVP.all() the wrong way I'm happy to receive tips!
function createItemList(fileList) {
var promise = new RSVP.Promise(function(resolve, reject) {
var itemList = [];
//For each file in fileList, get the directly accessible properties
//and push it to a new array
for (var i = 0, file; file = fileList[i]; i++) {
currentItem.Name = file.Name;
currentItem.LastModified = new Date(file.TimeLastModified).format("dd-MM-yyyy hh:mm:ss");
currentItem.Version = file.MajorVersion + "." + file.MinorVersion;
itemList.push(currentItem);
}
//This is where it starts to get messy...
//I want to map each item in the new itemlist to the corresponding properties
//in the fileList. If I can include the corresponding file somehow, I could set the
//data in the method 'getModifiedBy' and similar. I believe this should work,
//but I have no idea how I can achieve this.
var modifiedBy = itemList.map(function(item) {
return getModifiedBy(item, fileList[INDEX]);
});
var checkedOutBy = itemList.map(function (item) {
return getCheckedOutBy(item, fileList[INDEX]);
});
var eventDate = itemList.map(function (item) {
return getEventDate(item, fileList[INDEX]);
});
var promises = {
promisesModifiedBy: modifiedBy,
promisesCheckedOutBy: checkedOutBy,
promisesEventDate: eventDate
};
RSVP.all(promises)
.then(function() {
resolve(itemList);
});
});
return promise;
}
Use only a single map over the itemlist that returns a Promise for your 3-property-object. Use a dedicated helper function for single items.
Btw, with new RSVP.Promise you're using the deferred antipattern while there's absolutely no reason - RSVP.all() already returns you the result promise.
function createItemList(fileList) {
return RSVP.all(fileList.map(createItem));
}
function createItem(file) {
// get the directly accessible properties
var currentItem = {};
currentItem.Name = file.Name;
currentItem.LastModified = new Date(file.TimeLastModified).format("dd-MM-yyyy hh:mm:ss");
currentItem.Version = file.MajorVersion + "." + file.MinorVersion;
return RSVP.hash({
modifiedBy: getModifiedBy(currentItem, file),
checkedOutBy: getCheckedOutBy(currentItem, file)
eventDate: getEventDate(currentItem, file)
}).then(function(asyncprops) {
// somehow mix the asyncprops with the currentItem
return item; // into a result item
});
}

Asynchronous bulk find or create with ember.js

How can I perform a bulk find or create with ember.js? This would be simple to do synchronously (foreach... continue if exists). But working with ember's asynchronous store creates lots of overhead in keeping track of the state of the operation.
Specifically, I have a variable to keep track of the number of objects waiting to be processed (createIfNotExistTaskCounter), so I can check when the store has finished working on all of the objects to be saved. And I use an array to keep track of the items stored so far (createIfNotExistQueue) - I can't let the store handle this task, because I can't count on an item being found after it has been saved.
Here's my best solution below (also on JS Bin). Is there an easier way to do this?
App = Ember.Application.create({});
App.LSAdapter = DS.LSAdapter.extend({
namespace: 'whitespace'
});
App.Store = DS.Store.extend({
adapter: App.LSAdapter
});
App.Fruit = DS.Model.extend({
name: DS.attr("string")
});
App.IndexRoute = Ember.Route.extend({
createIfNotExistTaskCounter: 0, // store number of items waiting to be processed
createIfNotExistQueue: [], // store a list of the items being added, to prevent duplicate adds
setupController: function(controller) {
/* This is a simplified version of a real task I'm trying to acomplish. The code adds a list of objects to the store, only creating them if they don't exist. After the list has been processed, the contents of the store are shown.
To achieve this end I've used a counter and a queue to keep track of the operations' state. Is there a simpler way to do this? These overheads seem excessive for such a straightforward bulk insert operation.
*/
var fruitToStore = ["apple", "pear", "banana", "apple"],
store = this.get('store');
this.set('createIfNotExistTaskCounter', fruitToStore.length);
for(var i=0; i<fruitToStore.length; i++) {
this.createIfNotExist(fruitToStore[i]);
}
},
createListener: function() {
if(this.get('createIfNotExistTaskCounter') !== 0) return;
this.get('store').find('fruit').then(function(results) {
// should only print three fruits, one of each type
for (var i = 0; i < results.content.length; i++) {
console.log(results.content[i].get('name'));
};
});
}.observes('createIfNotExistTaskCounter'),
createIfNotExist: function(f) {
var store = this.get('store'),
queue = this.get('createIfNotExistQueue'),
that = this;
// prevent duplicate records being created by adding every (distinct) item to a queue
// the queue is used because there seems to be no way to tell if an item is already (asynchonously) being found / created / saved
if(queue.indexOf(f) !== -1) {
that.decrementProperty('createIfNotExistTaskCounter');
return;
}
queue.push(f);
// find or create
store.find('fruit', {name: f}).then(function(results) {
// found...
if(results.get('length') !== 0) {
that.decrementProperty('createIfNotExistTaskCounter');
return;
}
// ...else create
var fruit = store.createRecord('fruit', {name: f});
fruit.save().then(function() {
that.decrementProperty('createIfNotExistTaskCounter');
}, function() {
console.log("save failed!");
});
});
}
});
If you return a promise from a then callback, you can create a chain of promises that behaves quite like a queue.
First you start with an already resolved callback, then you keep replacing it with a "then"-able object.
queue: new Ember.RSVP.resolve,
addToQueue: function() {
this.queue = this.queue.then(function() {
return new Ember.RSVP.Promise(function(resolve, reject){
// something that eventually calls resolve
})
})
}
Here's my updated JSBin of your code: http://jsbin.com/OtoZowI/2/edit?html,console
There is probably a way to make that much smaller if you can work out a way to return the existing find / save promises instead of creating a new one. I played with it a bit but I need to get back to work :P
Also, you can collect together a bunch of promises with RSVP.all and resolve only once they're all resolved. Depending on your actual code, this might be a much cleaner solution - do all the finds, wait until they're all resolved, then create the missing objects.
The RSVP docs have a good example of that here: https://github.com/tildeio/rsvp.js/blob/master/README.md#arrays-of-promises

Ember.js how to concat two objects

I have an object in Ember (let's call it existing):
var existing = {
items: [
...
],
...
}
On the callback of hitting a server side request, there is a new response called result which looks the same as existing. I need to take the items in existing, and prepend them to the new result. So I have the following:
var result = { ... };
var existing = this.get('content');
result.items = result.items.concat(existing.items);
this.set('content', result);
The problem is, when rendered in the template, it is just displaying the new items from result, the old items from existing are not being displayed even though are in items. Any ideas why?
Thanks.
I guess the problem is that using concat which is not supported in Ember.Array (http://emberjs.com/api/classes/Ember.Array.html) does not trigger bindings and therefore your view is not updated. To get the items arrays merged you could do something like:
var result = { ... };
var existing = this.get('content');
result.items.forEach(item) {
existing.items.pushObject(item);
}
this.set('content', existing);
Hope it helps
If result.items is an array and you want to use the javascript concat method you can call toArray on the existing.items enumerable.
var result = { ... };
var existing = this.get('content');
result.items = result.items.concat(existing.get('items').toArray());
this.set('content', result);
If result.items is already an Ember.Enumerable I would use pushObjects or addObjects. http://emberjs.com/api/classes/Ember.MutableArray.html#method_addObjects
var result = { ... };
var existing = this.get('content.items');
result.items = result.get('items').addObjects(existing);
this.set('content', result);
I see this is an old post, but since there isn't an accepted answer, I thought I'd throw in what I feel is the best option. I recently found myself needing to concat a bunch of DS.ManyArray together and this was the solution I came up w/:
var myArrayOfHasManyArrays = ...;
var concatenated = [].concat.apply([], myArrayOfHasManyArrays.invoke('toArray'));
Hope this helps!

Categories