> For the complete documentation index, see [llms.txt](https://demirels-organization.gitbook.io/javascript-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://demirels-organization.gitbook.io/javascript-tutorial/writing-a-function-that-returns-a-promise/party-game-example.md).

# Party game example

Here is an example of a function that returns a promise in the context of a party computer game:

```javascript
function startNewGame(gameType) {
  return new Promise((resolve, reject) => {
    if (gameType === 'trivia') {
      console.log('Starting a new trivia game...');
      // simulate game setup with a 2 second delay
      setTimeout(() => {
        resolve();
      }, 2000);
    } else if (gameType === 'charades') {
      console.log('Starting a new charades game...');
      // simulate game setup with a 2 second delay
      setTimeout(() => {
        resolve();
      }, 2000);
    } else {
      reject(new Error('Invalid game type'));
    }
  });
}

startNewGame('trivia')
  .then(() => {
    console.log('Trivia game started successfully!');
  })
  .catch((error) => {
    console.log(error.message);
  });
```

This function takes in a `gameType` argument, which can be either 'trivia' or 'charades'. If the `gameType` is valid, the function will "start" the game by simulating a 2 second delay (using `setTimeout`) to represent the setup process. If the `gameType` is invalid, the function will reject the promise with an error.

The function returns a promise that is resolved or rejected based on the outcome of the game setup process. The calling code can use the `then` and `catch` methods to handle the resolved or rejected promise, respectively.

I hope this helps! Let me know if you have any questions.
