Complex Q promise: one promise creates an array of other promises - javascript

I have an http request that should return a list of tasks. However, those tasks are generated in a complex fashion. This is how it works.
Get all current tasks from the DB
expire tasks that are old
get user profiles from the DB
if the user doesn't have a profile and a task for creating the profile doesn't exist, add a task for creating the profile
additionally, for every subprofile the user has, make a daily task and save it to the DB, if a daily task hasn't already been created.
return all the tasks to the HTTP caller
I'm listing this all here, in case there's a better way to do it. From what I understand, I should have promises for both the DB calls that are then followed by promises that manipulate the task/profile data.
What I don't understand is how to add the N promises that are needed for daily tasks into my promise chain. I also need all the data available the final process to return the newly created list of tasks. Should I be nesting promises somehow?
Currently, I imagine it being something like this:
var taskPromise = dbPromise(serverName, taskRequest, params);
var profilesPromise = dbPromise(serverName, profilesRequest, params);
Q.all([taskPromise, profilesPromise])
.then(function(arrayOfTasksAndProfiles){
//do calculations and create an object like {tasks:[], profile:profile, subprofiles:[]})
.then(function(currentDataObject) {
var deferred = Q.defer();
var newTasksToBeCreated = // make a list of all the new tasks I want to create
var promisesForNewTasks = [] // create an array of promises that save each of the new tasks to the server
Q.all(promisesForNewTasks)
.then(function(returnedIDsForNewTasks) {
// somehow match the returned IDs to the newTasksToBeCreated and add them on
currentDataObject.newTasks = newTasksToBeCreated
deferred.resolve(currentDataObject);
});)
.then(function(currentDataObject) {
// now that the currentDataObject has all the tasks from the DB, plus the new ones with their IDs, I can respond with that information
res.json(currentDataObject))
.done();
I have to make multiple calls to the DB to create new tasks, and I need to return those appended to the other tasks I received from the DB, and the only way I can see to do that is to nest a Q.all() call.
"There's gotta be a better way."

Only one thing: Don't create a custom deferred that you need to manually resolve. Instead, just return from the then handler; and return the resulting promise of the .then() call.
.then(function(currentDataObject) {
var newTasksToBeCreated = // make a list of all the new tasks I want to create
var promisesForNewTasks = [] // create an array of promises that save each of the new tasks to the server
return Q.all(promisesForNewTasks)
// ^^^^^^
.then(function(returnedIDsForNewTasks) {
// somehow match the returned IDs to the newTasksToBeCreated and add them on
currentDataObject.newTasks = newTasksToBeCreated
return currentDataObject;
// ^^^^^^
});
})
Else, it looks quite fine. If you have problems matching the returned ids to the tasks - don't do it that way. Instead, make each of the promisesForNewTasks resolve with its own task object (combined with the id?).

Related

why my javascript its use to long time to run?

I'm working with Cloud Functions for Firebase, and I get a timeout with some of my functions. I'm pretty new with JavaScript. It looks like I need to put a for inside a promise, and I get some problems. The code actually get off from for too early, and I think he make this in a long time. Do you have some way to improve this code and make the code faster?
exports.firebaseFunctions = functions.database.ref("mess/{pushId}").onUpdate(event => {
//first i get event val and a object inside a firebase
const original = event.data.val();
const users = original.uids; // THIS ITS ALL USERS UIDS!!
// so fist i get all users uids and put inside a array
let usersUids = [];
for (let key in users) {
usersUids.push(users[key]);
}
// so now i gonna make a promise for use all this uids and get token's device
//and save them inside a another child in firebase!!
return new Promise((resolve) => {
let userTokens = [];
usersUids.forEach(element => {
admin.database().ref('users/' + element).child('token').once('value', snapShot => {
if (snapShot.val()) { // if token exist put him inside a array
userTokens.push(snapShot.val());
}
})
})
resolve({
userTokens
})
}) // now i make then here, from get userTokens and save in another child inside a firebase database
.then((res) => {
return admin.database().ref("USERS/TOKENS").push({
userTokens: res,
})
})
})
You are making network requests with firebase, so maybe that's why it's slow. You are making one request per user, so if you have 100 ids there, it might as well take a while.
But there's another problem that I notice, that is: you are just resolving to an empty list. To wait for several promises, create an array of promises, and then use Promise.all to create a promise that waits for all of them in parallel.
When you call resolve, you have already done the forEach, and you have started every promise, but they have not been added to the list yet. To make it better, chance it to a map and collect all the returned promises, and then return Promise.all.

Abortable promises and using paged data from an API for an autocomplete text field

Disclaimer: I'm new to ES6 and promises in general so its possible my approach is fundamentally wrong.
The Problem
I have an api that returns paged data. That is it returns a certain number of objects for a collection and if there's more data it returns a Next property in the response body with the url to get the next set of data. The data will eventually feed the autocomplete on a text input.
To be specific, if my collection endpoint is called /Tickets the response would look like:
{
Next: "/Tickets?skip=100&take=100"
Items: [ ... an array of the first 100 items ... ]
}
My current solution to get all the ticket data would be to
Make a new promise for the returning the whole combined set of data
"Loop" the ajax calls by chaining dones until there is no more Next value
Resolve the promise
getTickets(filterValue) {
let fullSetPromise = new Promise();
let fullSet = [];
// This gets called recursively
let append = function(previousResult) {
fullSet.concat(previousResult.Items);
// Loop!
if(previousResult.Next) {
$.ajax({
url: previousResult.Next
})
.done(append)
.catch(reason => fullSetPromise.reject(reason));
}
else {
fullSetPromise.resolve(fullSet);
}
}
// We set things off by making the request for the first 100
$.ajax({
url: `/Tickets?skip=0&take=100&filter=${filterValue}`
})
.done(append)
.catch(reason => fullSetPromise.reject(reason));
return fullSetPromise;
}
Eventually the promise is used by the frontend for autocomplete on a text input. Ideally I'd like to be able to abort the previous call when new input comes in.
inputChanged(e) {
this.oldTicketPromise.abort();
this.oldTicketPromise =
Api.GetTickets(e.target.value).then(updateListWithResults);
}
I am aware of debouncing. But that just means the problem happens every n seconds instead of on every key press.
I know the jqxhr object has an abort() property on it and I'd like that to be available to the caller somehow. But because there are multiple jqXHR objects used in GetTickets I'm not sure how to handle this.
So my main 2 questions are:
What is the appropriate way to consume paged data from an api while returning a promise.
How can the returned promise be made abortable?
Side question:
I feel like if I don't catch the errors then my "wrapper" promise will swallow any thrown errors. Is that a correct assumption?
Note the javascript code might have errors. It's mostly demonstrative for the logic.
Edit: Solved
I have solved this by combining this answer https://stackoverflow.com/a/30235261/730326 with an array of xhrs as suggested in the comments. I will add a proper answer with code when I find the time.

Need data from one asynchronous API call to make more, how do I build embedded promises in Angular?

The issue
I have 5 work queues and each queue has 5 tasks.
First, I make an API call to get all work queues. I then send an ID from each queue to another endpoint which gets me the tasks for that queue.
I'm trying to push all tasks from all queues into one array, so ideally the end product would be an array of 25 tasks.
Due to Angular's asynchronous nature, I'm having trouble getting all the data into the right place at the right time.
My current code
var getAllTasks = function () {
var deferred = $q.defer();
var allTasks = [];
qSrv.getQueues().then(function (response) {
// Loop through all queues
response.data.forEach(function (e, i) {
// Get Queue task list by passing in Global Unique Identifier to endpoint
qSrv.getQueueByQueueGuid(e.workQueueGuid).then(function (queue) {
// Push all tasks within task list to one array
queue.data.taskList.forEach(function (ee, ii) {
ee.queueName = e.name;
allTasks.push(ee);
});
});
});
// Resolve promise with arrays of all 25 tasks
deferred.resolve(allTasks);
})
return deferred.promise;
}
Conclusion
I know there's something funky going on with the promises not finishing before being passed along to the next step, but I don't know how to structure it to avoid this problem.
I would appreciate any and all help, thank you.

JavaScript work queue

I've created this object which contains an array, which serves as a work queue.
It kind of works like this:
var work1 = new Work();
var work2 = new Work();
var queue = Workqueue.instance();
queue.add(work1) // Bluebird promise.
.then(function addWork2() {
return queue.add(work2);
})
.then(function toCommit() {
return queue.commit();
})
.then(function done(results) {
// obtain results here.
})
.catch(function(err){});
It works in that case and I can commit more than one task before I call the commit.
However if it's like this:
var work1 = new Work();
var work2 = new Work();
var queue = Workqueue.instance();
queue.add(work1)
.then(function toCommit1() {
return queue.commit();
})
.then(function done1(result1) {
// obtain result1 here.
})
.catch(function(err){});
queue.add(work2)
.then(function toCommit2() {
return queue.commit();
})
.then(function done2(result2) {
// obtain result2 here.
})
.catch(function(err){});
Something may go wrong, because if the first commit is called after the second commit (two works/tasks are already added), the first commit handler expects a result but they all go to the second commit handler.
The task involves Web SQL database read and may also involves network access. So it's basically a complicated procedure so the above described problem may surface. If only I can have a addWorkAndCommit() implemented which wraps the add and commit together, but still there is no guarantee because addWorkAndCommit() cannot be "atomic" in a sense because they involves asynchronous calls. So even two calls to addWorkAndCommit() may fail. (I don't know how to describe it other than by "atomic", since JavaScript is single-threaded, but this issue crops up).
What can I do?
The problem is that there is a commit() but no notion of a transaction, so you cannot explicitly have two isolated transactions running in parallel. From my understanding the Javascript Workqueue is a proxy for a remote queue and the calls to add() and commit() map directly to some kind of remote procedure calls having a similar interface without transactions. I also understand that you would not care if the second add() actually happened after the first commit(), you just want to write two simple subsequent addWorkAndCommit() statements without synchronizing the underlying calls in client code.
What you can do is write a wrapper around the local Workqueue (or alter it directly if it is your code), so that each update of the queue creates a new transaction and a commit() always refers to one such transaction. The wrapper then delays new updates until all previous transactions are committed (or rolled back).
Adopting Benjamin Gruenbaum's recommendation to use a disposer pattern, here is one, written as an adapter method for Workqueue.instance() :
Workqueue.transaction = function (work) { // `work` is a function
var queue = this.instance();
return Promise.resolve(work(queue)) // `Promise.resolve()` avoids an error if `work()` doesn't return a promise.
.then(function() {
return queue.commit();
});
}
Now you can write :
// if the order mattters,
// then add promises sequentially.
Workqueue.transaction(function(queue) {
var work1 = new Work();
var work2 = new Work();
return queue.add(work1)
.then(function() {
return queue.add(work2);
});
});
// if the order doesn't mattter,
// add promises in parallel.
Workqueue.transaction(function(queue) {
var work1 = new Work();
var work2 = new Work();
var promise1 = queue.add(work1);
var promise2 = queue.add(work2);
return Promise.all(promise1, promise2);
});
// you can even pass `queue` around
Workqueue.transaction(function(queue) {
var work1 = new Work();
var promise1 = queue.add(work1);
var promise2 = myCleverObject.doLotsOfAsyncStuff(queue);
return Promise.all(promise1, promise2);
});
In practice, an error handler should be included like this - Workqueue.transaction(function() {...}).catch(errorHandler);
Whatever you write, all you need to do is ensure that the callback function returns a promise that is an aggregate of all the component asynchronisms (component promises). When the aggregate promise resolves, the disposer will ensure that the transaction is committed.
As with all disposers, this one doesn't do anything you can't do without it. However it :
serves as a reminder of what you are doing by providing a named .transaction() method,
enforces the notion of a single transaction by constraining a Workqueue.instance() to one commit.
If for any reason you should ever need to do two or more commits on the same queue (why?), then you can always revert to calling Workqueue.instance() directly.

Promise based queue

I have an array of files, that I'd like to attack N at a time. And a function doWork that returns a promise.
var files = []
var doWork = function(file) {
return asyncFn(file)
}
I'd like to be able to push onto this queue dynamically.
Edit: I've tried various modules (promise-queue, async-q). They all work in a fashion, but they don't allow using an array as a queue. They have there own internal structure that you need to push onto.
The reason I need to use an array as I want to be able to push an item onto the queue, and check that it's not already on the queue.
Here is how you would do that with Bluebird which you indicated you were using.
var files = ["foo.txt", "bar.txt", "baz.txt"];
var task = Promise.map(files, doWork, {concurrency: 4}); // four at a time
task.then(function(results){
// results contains the results, tasks are executed at most 4 at a time
});
A word of caution - this puts an upper limit on how much the current invocation will run, calling the function multiple times, or from multiple node processes will (obviously) execute with larger/smaller concurrency. However in the simple case - this works.
You could do something like this:
function enq (step)
var f = function() {
var d = Q.defer();
step(d);
return d.promise;
}
enq_head = enq_head.then(f);
}
where step is a function that fulfills the promise you pass it. But I don't recommend it cos it's just a fancy way of doing what setTimeout does much more efficiently.
If you want to keep track of which files you've scheduled and/or completed, just put them in a done list or take them out of the todo list you get them from, stick a bool in an object under the filename or whatever. It's a seperate problem from the scheduling.

Categories