Skip to content

Instantly share code, notes, and snippets.

@queerviolet
Last active February 9, 2025 15:33
Show Gist options
  • Save queerviolet/7d9fb275b292b062fa5b9b4c99eda77d to your computer and use it in GitHub Desktop.
Save queerviolet/7d9fb275b292b062fa5b9b4c99eda77d to your computer and use it in GitHub Desktop.
Player inquirer loop for choose your own adventure.
"use strict";
var inquirer = require('inquirer');
var game = require('./game.source');
// play(node: Node) -> Promise<Node>
function play(node) {
if (!node.connections.length) {
console.log(node.text);
return Promise.resolve({node}); // Note: `{node}` is ES6 object shorthand syntax. ES5 would be Promise.resolve({node: node})
}
return inquirer.prompt({
name: 'node',
message: node.text,
type: 'list',
choices: node.connections,
}).then(answer => play(answer.node)); // Note: This is an ES6 Arrow function.
// ES5 would be `.then(function(answer) { return play(answer.node); })`
}
play(game.startingPoint)
.then(last => console.log('Game over.'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment