/    Sign up×
Community /Pin to ProfileBookmark

Parallel work in javascript, how to return in the right order?

My class is to execute parallel tasks in accordance with the code specified parameter parallelJobs.

I need to return responses in the right order.

example code([URL=”http://codepen.io/anon/pen/LbbNaK?editors=0012″]codepen[/URL]):

[CODE]
class Parallel {
constructor(number) {
this.parallelJobs = number.parallelJobs;
this.results = [];
this.currentJobs = 0;
this.counterCalls = 0;
this.countJobCalls = 0;
}

pushArray(value) {
if (this.results.length && this.results.length !== this.counterCalls) {
setTimeout(() => {
this.pushArray(value, callNumber)
}, 50);
} else {

this.results.push(value);
this.counterCalls++;
}

this.currentJobs–;
return true;
}

runFunc(runConsole) {
this.currentJobs++;
runConsole(this.pushArray.bind(this));
}

job(runConsole) {
this.countJobCalls++;
if (this.currentJobs < this.parallelJobs) {
this.runFunc(runConsole);
} else {
setTimeout(() => {
this.job(runConsole);
}, 500);
}

return this;
}

done(onDone) {
this.lastJobCall = this.countJobCalls;
if (this.results.length && this.lastJobCall === this.results.length) {
onDone(this.results);
} else {
setTimeout(() => {
this.done(onDone)
}, 50);
}

return true;
}
}

var runner = new Parallel({
parallelJobs: 4
});

runner.job(step1)
.job(step2)
.job(step3)
.job(step4)
.done(onDone);

function step1(done) {
console.log(‘step1’);
setTimeout(done, 100, ‘step1’);
}

function step2(done) {
console.log(‘step2’);
setTimeout(done, 10, ‘step2’);
}

function step3(done) {
console.log(‘step3’);
setTimeout(done, 150, ‘step3’);
}

function step4(done) {
console.log(‘step4’);
setTimeout(done, 50, ‘step4’);
}

function onDone(results) {
console.log(results);
}
There should return an array [“step1”, “step2”, “step3”, “step4”] what best way this can be achieved in this example?[/CODE]

There should return an array [“step1”, “step2”, “step3”, “step4”] what best way this can be achieved in this example?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rootNov 18.2016 — I would stuff the array with objects that contain the job number (which should be sequential) the job or data about that job, perform a sort on the array by the job number in the object...
Copy linkTweet thisAlerts:
@SempervivumNov 18.2016 — Seems to be Java, not Javascript. AFAIK there is no parallel processing in Javascript.
×

Success!

Help @d_doe spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...