jmar777 / suspend

Callback-free control flow for Node using ES6 generators.
548 stars 22 forks source link

Support for context data associated with parallel tasks, supplied to fork() operation #32

Closed beyondcompute closed 3 years ago

beyondcompute commented 8 years ago

Hello! Thank you very much for your amazing project. 😊 In our environment however we're using suspend to run a mix of different tasks (db queries, file reads) in parallel. For that sake it is helpful for us to associate some data with the task so that later we can access the context in which result was obtained.

Here's our proposal:

files.forEach(function(filepath) {
    fs.readFile(filepath, suspend.fork({filepath: filepath}));
});
var results = yield suspend.join();
// [ { data: { filepath: 'path/to/file/3' },
//    value: <Buffer ... > },
//  { data: { filepath: 'path/to/file/1' },
//    value: <Buffer ... > },
//  { data: { filepath: 'path/to/file/2' },
//    value: <Buffer ... > } ]

I hope this function has a chance to make it to the upstream. I would be very glad to hear your opinion about the matter. Maybe we could create a new method like forkContext() or forkData() to make the change as robust as possible in respect to backward compatibility. Thank you and have a good day!