Commit 7b9afcda authored by David Votrubec's avatar David Votrubec

Using simpler version of spawn() function

parent e2edb915
"use strict";
/**
* Create a `spawn` function to deal with promises
* Inspired by https://gist.github.com/dtothefp/a0a6a5c2e3dae8bc0d0f
*/
module.exports = function spawn(generatorFn) {
const iterator = generatorFn(); //instantiate the generator and return the iterator object
// create helper function which will call itself repeatedly
function _co(method, arg) {
let result;
try {
// retrieve the promise if `arg` is undefined (eg: returned by the http request)
// if `arg` is defined it will be the data from the promise
// and will be "injected" into `yield` and caught in a variable
result = iterator[method](arg); //method is either 'next' or 'throw'
} catch(err) {
return Promise.reject(err);
}
if (result.done) {
if (method === 'throw') {
return arg;
} else {
return result.value;
}
} else {
// Using the static Promise.resolve() method
// it will convert the result.value into a Promise object
// making it thenable.
// If there was an exception it will rethrown via _co('throw', err)
return Promise.resolve(result.value)
.then((val) => {
return _co('next', val);
}, (err) => {
return _co('throw', err);
});
}
}
// start the process by calling `.next` on the iterator instance
return _co('next');
};
\ No newline at end of file
// "use strict";
// /**
// * Create a `spawn` function to deal with promises
// * Inspired by https://gist.github.com/dtothefp/a0a6a5c2e3dae8bc0d0f
// */
// module.exports = function spawn(generatorFn) {
// const iterator = generatorFn(); //instantiate the generator and return the iterator object
// // create helper function which will call itself repeatedly
// function _co(method, arg) {
// let result;
// try {
// // retrieve the promise if `arg` is undefined (eg: returned by the http request)
// // if `arg` is defined it will be the data from the promise
// // and will be "injected" into `yield` and caught in a variable
// result = iterator[method](arg); //method is either 'next' or 'throw'
// } catch(err) {
// return Promise.reject(err);
// }
// if (result.done) {
// if (method === 'throw') {
// return arg;
// } else {
// return result.value;
// }
// } else {
// // Using the static Promise.resolve() method
// // it will convert the result.value into a Promise object
// // making it thenable.
// // If there was an exception it will rethrown via _co('throw', err)
// return Promise.resolve(result.value)
// .then((val) => {
// return _co('next', val);
// }, (err) => {
// return _co('throw', err);
// });
// }
// }
// // start the process by calling `.next` on the iterator instance
// return _co('next');
// };
// simpler version of spawn() from https://tc39.github.io/ecmascript-asyncawait/#desugaring
// much easier to read and reason about
module.exports = function spawn(genF, self) {
return new Promise(function(resolve, reject) {
var gen = genF.call(self);
function step(nextF) {
var next;
try {
next = nextF();
} catch(e) {
// finished with failure, reject the promise
reject(e);
return;
}
if (next.done) {
// finished with success, resolve the promise
resolve(next.value);
return;
}
// not finished, chain off the yielded promise and `step` again
Promise.resolve(next.value).then(function(v) {
step(function() { return gen.next(v); });
}, function(e) {
step(function() { return gen.throw(e); });
});
}
step(function() { return gen.next(undefined); });
});
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ const readFile = thunkify(fs.readFile);
run(function* () {
try {
const fileContent = yield readFile('./es6/blabla.json');
console.log(fileContent);
console.log(fileContent); // binary array, but we do not mind :)
}
catch (er) {
console.error(er);
......
......@@ -20,7 +20,9 @@ spawn(function*(){
}
});
// helper function
/////////////////////////////////////////////////////////////////
// helper functions
/////////////////////////////////////////////////////////////////
function getTimeoutPromise(timeout, resultValue, shouldReject) {
const promise = new Promise(function(resolve, reject) {
setTimeout(function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment