/1.js
Last active
June 17, 2016 16:37
Revisions
-
getify revised this gist
May 28, 2016 . No changes.There are no files selected for viewing
-
getify created this gist
May 28, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ // commented `run(..)`: function run(it) { // return a promise for the generator completing return Promise.resolve() .then( function handleNext(value){ // run to the next yielded value var next = it.next( value ); return (function handleResult(next){ // generator has completed running? if (next.done) { return next.value; } // otherwise keep going else { return Promise.resolve( next.value ) .then( // resume the async loop on // success, sending the resolved // value back into the generator handleNext, // if `value` is a rejected // promise, propagate error back // into the generator for its own // error handling function handleErr(err) { return Promise.resolve( it.throw( err ) ) .then( handleResult ); } ); } })(next); } ); } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ // un-commented `run(..)`: function run(it) { return Promise.resolve() .then( function handleNext(value){ var next = it.next( value ); return (function handleResult(next){ if (next.done) { return next.value; } else { return Promise.resolve( next.value ) .then( handleNext, function handleErr(err) { return Promise.resolve( it.throw( err ) ) .then( handleResult ); } ); } })(next); } ); } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ // ... with some ES6 sugar function run(it) { return Promise.resolve() .then( function handleNext(value){ var next = it.next( value ); return (function handleResult(next){ return next.done ? next.value : Promise.resolve( next.value ) .then( handleNext, err => Promise.resolve( it.throw( err ) ) .then( handleResult ) ); } })( next ); } ); }