Add support for async ops using async/await

This commit is contained in:
toby 2017-04-21 17:48:42 -04:00
parent 07bb095e73
commit c39622ed1e
5 changed files with 11 additions and 11 deletions

View File

@ -34,7 +34,7 @@ var Chef = function() {
* @returns {number} response.duration - The number of ms it took to execute the recipe * @returns {number} response.duration - The number of ms it took to execute the recipe
* @returns {number} response.error - The error object thrown by a failed operation (false if no error) * @returns {number} response.error - The error object thrown by a failed operation (false if no error)
*/ */
Chef.prototype.bake = function(inputText, recipeConfig, options, progress, step) { Chef.prototype.bake = async function(inputText, recipeConfig, options, progress, step) {
var startTime = new Date().getTime(), var startTime = new Date().getTime(),
recipe = new Recipe(recipeConfig), recipe = new Recipe(recipeConfig),
containsFc = recipe.containsFlowControl(), containsFc = recipe.containsFlowControl(),
@ -72,7 +72,7 @@ Chef.prototype.bake = function(inputText, recipeConfig, options, progress, step)
} }
try { try {
progress = recipe.execute(this.dish, progress); progress = await recipe.execute(this.dish, progress);
} catch (err) { } catch (err) {
// Return the error in the result so that everything else gets correctly updated // Return the error in the result so that everything else gets correctly updated
// rather than throwing it here and losing state info. // rather than throwing it here and losing state info.

View File

@ -38,7 +38,7 @@ const FlowControl = {
* @param {Operation[]} state.opList - The list of operations in the recipe. * @param {Operation[]} state.opList - The list of operations in the recipe.
* @returns {Object} The updated state of the recipe. * @returns {Object} The updated state of the recipe.
*/ */
runFork: function(state) { runFork: async function(state) {
var opList = state.opList, var opList = state.opList,
inputType = opList[state.progress].inputType, inputType = opList[state.progress].inputType,
outputType = opList[state.progress].outputType, outputType = opList[state.progress].outputType,
@ -73,7 +73,7 @@ const FlowControl = {
for (i = 0; i < inputs.length; i++) { for (i = 0; i < inputs.length; i++) {
var dish = new Dish(inputs[i], inputType); var dish = new Dish(inputs[i], inputType);
try { try {
progress = recipe.execute(dish, 0); progress = await recipe.execute(dish, 0);
} catch (err) { } catch (err) {
if (!ignoreErrors) { if (!ignoreErrors) {
throw err; throw err;

View File

@ -145,7 +145,7 @@ Recipe.prototype.lastOpIndex = function(startIndex) {
* @param {number} [startFrom=0] - The index of the Operation to start executing from * @param {number} [startFrom=0] - The index of the Operation to start executing from
* @returns {number} - The final progress through the recipe * @returns {number} - The final progress through the recipe
*/ */
Recipe.prototype.execute = function(dish, startFrom) { Recipe.prototype.execute = async function(dish, startFrom) {
startFrom = startFrom || 0; startFrom = startFrom || 0;
var op, input, output, numJumps = 0; var op, input, output, numJumps = 0;
@ -170,11 +170,11 @@ Recipe.prototype.execute = function(dish, startFrom) {
"numJumps" : numJumps "numJumps" : numJumps
}; };
state = op.run(state); state = await op.run(state);
i = state.progress; i = state.progress;
numJumps = state.numJumps; numJumps = state.numJumps;
} else { } else {
output = op.run(input, op.getIngValues()); output = await op.run(input, op.getIngValues());
dish.set(output, op.outputType); dish.set(output, op.outputType);
} }
} catch (err) { } catch (err) {

View File

@ -73,11 +73,11 @@ App.prototype.handleError = function(err) {
* @param {boolean} [step] - Set to true if we should only execute one operation instead of the * @param {boolean} [step] - Set to true if we should only execute one operation instead of the
* whole recipe. * whole recipe.
*/ */
App.prototype.bake = function(step) { App.prototype.bake = async function(step) {
var response; var response;
try { try {
response = this.chef.bake( response = await this.chef.bake(
this.getInput(), // The user's input this.getInput(), // The user's input
this.getRecipeConfig(), // The configuration of the recipe this.getRecipeConfig(), // The configuration of the recipe
this.options, // Options set by the user this.options, // Options set by the user

View File

@ -40,13 +40,13 @@ import Chef from "../src/core/Chef.js";
this.tests.map(function(test, i) { this.tests.map(function(test, i) {
var chef = new Chef(); var chef = new Chef();
return Promise.resolve(chef.bake( return chef.bake(
test.input, test.input,
test.recipeConfig, test.recipeConfig,
{}, {},
0, 0,
false false
)) )
.then(function(result) { .then(function(result) {
var ret = { var ret = {
test: test, test: test,