Skip to content

Instantly share code, notes, and snippets.

@gaurang847
Last active June 19, 2019 19:43
Show Gist options
  • Save gaurang847/a6804f916f570333e6fdf1c764c6c2f9 to your computer and use it in GitHub Desktop.
Save gaurang847/a6804f916f570333e6fdf1c764c6c2f9 to your computer and use it in GitHub Desktop.
//Considering getValueFromFile1, getValueFromFile2 and getValueFromFile3
//all return promises
function* coroutine(){ //this part looks
let a = yield getValueFromFile1(); //very similar to
let b = yield getValueFromFile2(); //the synchronous
let sum = a + b; //equivalent
yield printSumToFile3(sum);
console.log('Success');
}
let co = coroutine();
co.next().value //getValueFromFile1() called; which returns a promise
.then(result => co.next(result).value) //result stored in variable 'a' and getValueFromFile2() called
.then(result => co.next(result).value) //result stored in variable 'b' and printSumToFile3() called
.then(result => co.next(result).value); //print 'Success'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment