Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //run this in node.js, firefox only, not in browser chrome for now
- const parallel = async (...items) => {
- const temp = [];
- for (const item of items) {
- temp.push(await item);
- }
- return temp;
- };
- const someResult = async () => {
- return new Promise((resolve, reject) => {
- console.log('running someResult() ...');
- setTimeout(() => {
- console.log('someResult() is done');
- resolve('hello');
- }, 2000);
- });
- };
- const anotherResult = async () => {
- return new Promise((resolve, reject) => {
- console.log('running anotherResult()...');
- setTimeout(() => {
- console.log('anotherResult() is done');
- resolve('world');
- }, 3000);
- });
- };
- (async () => {
- console.log('running test case');
- const t0 = new Date()
- const [p1, p2] = await parallel(someResult(), anotherResult());
- const t1 = new Date()
- console.assert(p1 == 'hello')
- console.assert(p2 == 'world')
- console.assert(t1 - t0 < 3010)
- console.log(p1, p2, t1 - t0)
- console.log('all assertion passed, someResult(), anoterResult() is executed in parallel')
- })();
- console.log('eof')
Add Comment
Please, Sign In to add comment