Skip to content

Instantly share code, notes, and snippets.

@vitalymak
Last active September 21, 2016 14:34
Show Gist options
  • Save vitalymak/a98c20eee61858ab59dfbdec61be1e69 to your computer and use it in GitHub Desktop.
Save vitalymak/a98c20eee61858ab59dfbdec61be1e69 to your computer and use it in GitHub Desktop.
Bluebird's coroutine example: using generators (yield) Promise.coroutine and Promise.coroutine.addYieldHandler
'use strict';
const Promise = require('bluebird');
let start = Date.now();
let promises = [1];
for (let i = 1; i <= 10; ++i) {
promises.push(() => new Promise(resolve => setTimeout(() => {
console.log(`#${i} completed in ${Date.now() - start}ms`);
resolve('Resolved value ' + i);
}, i * 100)))
}
let f = function* () {
console.log(1, 'Consider this is sync operation, arguments: ' + JSON.stringify(arguments));
console.log(yield promises[3]());
console.log(2);
yield promises[1]();
console.log(3);
yield promises[5]();
console.log(4);
yield promises[2]();
console.log(5);
console.log(`Parallel explicit run...`);
yield Promise.all([
promises[6](),
promises[7](),
promises[8](),
]);
return yield promises[4]();
};
let fFail1 = function* () {
yield Promise.reject(new Error('fFail1 - via Promise.reject'));
console.log('Never printed');
};
let fFail2 = function* () {
throw new Error('fFail2 - via throw');
console.log('Never printed');
};
// Returns a function that can use yield to yield promises
let fWrapped = Promise.coroutine(f);
let promise = fWrapped('arg1', 'arg2');
console.log(`promise instance of bluebird Promise is ${promise instanceof Promise}`);
promise
.finally(() => {
console.log('Finally handler');
})
.then(value => console.log(`Then.. value = ${value}`))
.catch(error => console.log(`Error.. error ${error}`));
Promise.coroutine(fFail1)()
.then(value => console.log(`fFail1 then.. value = ${value}`))
.catch(error => console.log(`fFail1 Error.. error ${error}`));
Promise.coroutine(fFail2)()
.then(value => console.log(`fFail2 then.. value = ${value}`))
.catch(error => console.log(`fFail2 Error.. error ${error}`));
// An example of handling promises in parallel by adding an addYieldHandler for arrays :
Promise.coroutine.addYieldHandler(function(yieldedValue) {
if (Array.isArray(yieldedValue)) return Promise.all(yieldedValue);
});
Promise.coroutine(function* () {
console.log(`Parallel implicit run...`);
yield [
promises[9](),
promises[10](),
];
})();
/* OUTPUT:
1 'Consider this is sync operation, arguments: {"0":"arg1","1":"arg2"}'
promise instance of bluebird Promise is true
Parallel implicit run...
fFail1 Error.. error Error: fFail1 - via Promise.reject
fFail2 Error.. error Error: fFail2 - via throw
#3 completed in 305ms
Resolved value 3
2
#1 completed in 406ms
3
#5 completed in 907ms
#9 completed in 907ms
4
#10 completed in 1007ms
#2 completed in 1112ms
5
Parallel explicit run...
#6 completed in 1712ms
#7 completed in 1813ms
#8 completed in 1918ms
#4 completed in 2319ms
Finally handler
Then.. value = Resolved value 4
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment