Skip to content

Instantly share code, notes, and snippets.

@mildfuzz
Created May 21, 2016 23:09
Show Gist options
  • Select an option

  • Save mildfuzz/9120db7571cc8c58abfb1ef41de0cc95 to your computer and use it in GitHub Desktop.

Select an option

Save mildfuzz/9120db7571cc8c58abfb1ef41de0cc95 to your computer and use it in GitHub Desktop.
Using a generator to trigger a sequence of promises
function* triggerGen(res, rej) {
const result = yield;
if (result) {
res(result);
} else {
rej();
}
}
var trigger;
const prom = new Promise((res, rej) => {
trigger = triggerGen(res, rej);
trigger.next();
});
prom
.then(function(x) {
console.log(x);
return Promise.resolve(x * 2)
})
.then(function(y) {
console.log(y);
return Promise.resolve(y * 2);
})
.then(function(z) {
console.log(z * 2);
});
setTimeout(function(){
trigger.next(2);
},1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment