Created
March 14, 2017 23:48
-
-
Save ppg/bd59c83380c5e2e00259f68dc140b10d to your computer and use it in GitHub Desktop.
Demonstrates how ES6 promises work with respect to traditional callback tasks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const async = require('async'); | |
// All fN functions should take n and m and return all permutations between the two; | |
// i.e. for 2, 3: | |
// ['0 - 0', '0 - 1', '0 - 2', '1 - 0', '1 - 1', '1 - 2'] | |
function workerTraditional(i, j, callback) { | |
// Put in a sleep | |
setTimeout(() => callback(null, i + '-' + j), 10); | |
} | |
function workerPromise(i, j) { | |
return new Promise((resolve) => { | |
// Put in a sleep | |
setTimeout(() => resolve(i + '-' + j), 10); | |
}); | |
} | |
/** | |
* Traditional callback tasks, traditional callback response | |
*/ | |
function f1(n, m, callback) { | |
let tasks = []; | |
for (let i = 0; i < n; i++) { | |
for (let j = 0; j < m; j++) { | |
tasks.push(cb => workerTraditional(i, j, cb)); | |
} | |
} | |
async.parallel(tasks, callback); | |
} | |
console.log('before f1'); | |
f1(2, 3, (err, results) => { | |
if (err) return console.log('f1 err', err); | |
console.log('f1 results', results); | |
}); | |
console.log('after f1 (work not done yet)'); | |
/** | |
* Traditional callback tasks, async.parallel, promise response | |
*/ | |
function f2(n, m) { | |
let tasks = []; | |
for (let i = 0; i < n; i++) { | |
for (let j = 0; j < m; j++) { | |
tasks.push(cb => workerTraditional(i, j, cb)); | |
} | |
} | |
return new Promise((resolve, reject) => { | |
async.parallel(tasks, (err, results) => err ? reject(err) : resolve(results)); | |
}); | |
// NOTE: workerTraditional tasks can start now | |
} | |
console.log('before f2'); | |
f2(2, 3) | |
.then(results => console.log('f2 results', results)) | |
.catch(err => console.log('f2 err', err)); | |
console.log('after f2 (work not done yet)'); | |
/** | |
* Traditional callback tasks, Promise.all | |
*/ | |
function f3(n, m) { | |
let promises = []; | |
for (let i = 0; i < n; i++) { | |
for (let j = 0; j < m; j++) { | |
let convertedPromise = new Promise((resolve, reject) => { | |
workerTraditional(i, j, (err, results) => err ? reject(err) : resolve(results)); | |
}); | |
// NOTE: workerTraditional can start now | |
promises.push(convertedPromise); | |
} | |
} | |
return Promise.all(promises); | |
} | |
console.log('before f3'); | |
f3(2, 3) | |
.then(results => console.log('f3 results', results)) | |
.catch(err => console.log('f3 err', err)); | |
console.log('after f3 (work not done yet)'); | |
/** | |
* Promise tasks, Promise.all | |
*/ | |
function f4(n, m) { | |
let promises = []; | |
for (let i = 0; i < n; i++) { | |
for (let j = 0; j < m; j++) { | |
promises.push(workerPromise(i, j)); | |
// NOTE: workerPromise can start now | |
} | |
} | |
return Promise.all(promises); | |
} | |
console.log('before f4'); | |
f4(2, 3) | |
.then(results => console.log('f4 results', results)) | |
.catch(err => console.log('f4 err', err)); | |
console.log('after f4 (work not done yet)'); | |
/** | |
* Promise tasks, traditional callback response | |
*/ | |
function f5(n, m, callback) { | |
let promises = []; | |
for (let i = 0; i < n; i++) { | |
for (let j = 0; j < m; j++) { | |
promises.push(workerPromise(i, j)); | |
// NOTE: workerPromise can start now | |
} | |
} | |
return Promise.all(promises) | |
.then(results => callback(null, results)) | |
// either catch will work | |
//.catch(err => callback(err)) | |
.catch(callback); | |
} | |
console.log('before f5'); | |
f5(2, 3, (err, results) => { | |
if (err) return console.log('f5 err', err); | |
console.log('f5 results', results); | |
}); | |
console.log('after f5 (work not done yet)'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment