
上QQ阅读APP看书,第一时间看更新
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named 3-04-chaining-promises.
- Copy or create an index.html that loads and runs a main function from main.js.
- Create a main.js file that creates a promise. Resolve a random number from the promise:
new Promise(function (resolve) { resolve(Math.random()); }); );
- Chain a then call off of the promise. Return true from the callback if the random value is greater than or equal to 0.5:
new Promise(function (resolve, reject) { resolve(Math.random()); }).then(function(value) {
return value >= 0.5;
});
- Chain a final then call after the previous one. Log out a different message if the argument is true or false:
new Promise(function (resolve, reject) { resolve(Math.random()); }).then(function (value) { return value >= 0.5; }).then(function (isReadyForLaunch) {
if (isReadyForLaunch) {
console.log('Start the countdown!');
} else {
console.log('Abort the mission.');
}
});
Start your Python web server and open the following link in your browser:
http://localhost:8000/.If you are lucky, you'll see the following output:
- If you are unlucky, we'll see the following output: