I have an event listener, which will receive event (from server) whenever a specific task is done. I desire each task to be encapsulated in a promise, so that I can construct complex ordered workflow in a clean manner.
How would I resolve each task promise (by id) from a global listener? Here's an conceptual example of what I am trying to achieve.
// list of all tasks
var tasks = {}
tasks['id-1'] = getTask('id-1')
tasks['id-2'] = getTask('id-2')
// do task in order
// each task will issue request to server, and wait for reply
tasks['id-1'].startPromise()
.then(function(result) {
// start second task from first task's data
return tasks['id-2'].startPromise(result);
})
.then(function() {
// finalized task works
return true;
});
// listen to task finish
client.listen('onReceive', function(event) {
if(event.is('taskFinished')) {
var id = event.taskId;
tasks[id].resolve(event.result); //How do I resolve task from listener??
}
});