How to do it...

  1. Open your command-line application and navigate to your workspace.
  2. Create a new folder named 04-03-async-function-Promise-chain.
  3. Copy or create an index.html that loads and runs a main function from main.js.
  1. Create an async function, getRandomNumber, that returns a random number:
async function getRandomNumber() { 
  console.log('Getting random number.'); 
  return Math.random(); 
} 
  1. Create an async function, determinReadyToLaunch, that returns true if its first argument is greater than 0.5:
async function deteremineReadyToLaunch(percentage) { 
  console.log('Determining Ready to launch.'); 
  return Math.random() > 0.5; 
}  
  1. Create a third async function, reportResults, that logs out different results if its first argument is true or false:
async function reportResults(isReadyToLaunch) { 
  if (isReadyToLaunch) { 
    console.log('Rocket ready to launch. Initiate countdown: '); 
  } else { 
    console.error('Rocket not ready. Abort mission: '); 
  } 
}  
  1. Create a main function that calls getRandomNumber, and a chain of the Promise it creates to call determineReadyToLaunch and reportResults in succession:
export function main() { 
  console.log('Before Promise created'); 
  getRandomNumber() 
    .then(deteremineReadyToLaunch) 
    .then(reportResults) 
  console.log('After Promise created'); 
}   
  1. Start your Python web server and open the following link in your browser: http://localhost:8000/.
  1. You should see the following output:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.159.235