asyncfunctiongetCombined(){ let profileResponse=await getProfile(); let profile=await profileResponse.json(); let reposResponse=await getRepos(); let repos= await reposResponse.json(); return { repos, profile }; }
var RP = require("request-promise"); var sites = awaitPromise.all([ RP("http://www.google.com"), RP("http://www.apple.com"), RP("http://www.yahoo.com") ])
asyncfunctiongetCombined(){ let profileResponse=await getProfile(); let profile=await profileResponse.json(); let reposResponse=await getRepos(); let repos= await reposResponse.json(); return { repos, profile }; }
asyncfunctiongetCombined(){ let profileResponse=await getProfile(); let profile=await profileResponse.json(); let reposResponse=await getRepos(); let repos= await reposResponse.json(); return { repos, profile }; }
promise .then((age)=>{ return`Your age is ${age}, so you can meet Cang Laoshi`; }) .then((msg)=>{ console.log(`Congratulations! ${msg}`); }) .then((msg)=>{ console.log("Please contact deshui.wang"); }); 输出:
Congratulations! Your age is 20, so you can meet Cang Laoshi Please contact deshui.wang
我们在then里面 也可以是一个异步操作,那么后面的then 将等待前一个promise完成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
promise .then((age)=>{ return`Your age is ${age}, so you can meet Cang Laoshi`; }) .then((msg)=>{ setTimeout(()=>{ console.log(`Congratulations! ${msg}`); },5000); }) .then((msg)=>{ console.log("Please contact deshui.wang"); }); 输出 Please contact deshui.wang Congratulations! Your age is 20, so you can meet Cang Laoshi
var name = "Brendan"; console.log(`Yo, ${name}!`);
// => "Yo, Brendan!"
表达式
1 2 3 4 5 6 7 8
var a = 10; var b = 10; console.log(`JavaScript first appeared ${a+b} years ago. Crazy!`);
//=> JavaScript first appeared 20 years ago. Crazy!
console.log(`The number of JS MVC frameworks is ${2 * (a + b)} and not ${10 * (a + b)}.`); //=> The number of JS frameworks is 40 and not 200.
函数
1 2 3
functionfn() { return"I am a result. Rarr"; } console.log(`foo ${fn()} bar`); //=> foo I am a result. Rarr bar.
$() 可以使用任何表达式和方法调用
1 2 3 4 5 6 7 8 9 10
var user = {name: 'Caitlin Potter'}; console.log(`Thanks for getting this into V8, ${user.name.toUpperCase()}.`);
// => "Thanks for getting this into V8, CAITLIN POTTER";
// And another example var thing = 'drugs'; console.log(`Say no to ${thing}. Although if you're talking to ${thing} you may already be on ${thing}.`);
// => Say no to drugs. Although if you're talking to drugs you may already be on drugs.
示例代码:
ES5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
'use strict';
var customer = { name: "Foo" }; var card = { amount: 7, product: "Bar", unitprice: 42 };
var message = "Hello " + customer.name + ",\n" + "want to buy " + card.amount + " " + card.product + " for\n" + "a total of " + (card.amount * card.unitprice) + " bucks?";
console.log(message);
输出:
Hello Foo, want to buy 7 Bar for a total of294 bucks?
ES6:
1 2 3 4 5 6 7 8 9 10 11
var customer = { name: "Foo" } var card = { amount: 7, product: "Bar", unitprice: 42 } message = `Hello ${customer.name}, want to buy ${card.amount}${card.product} for a total of ${card.amount * card.unitprice} bucks?`
输出:
Hello Foo, want to buy 7 Bar for a total of294 bucks?
let interpreted = 'raw\nstring'; let esaped = 'raw\\nstring'; let raw = String.raw`raw\nstring`; console.log(interpreted); // raw // string console.log(raw === esaped); // true
functionf(){ var saleCount; console.log(saleCount); if(saleCount<100) { // according some rule, change it to 100 saleCount=60; console.log(saleCount); }
}
f() // undefined
避免循环变量变为全局变量
1 2 3 4 5 6 7 8 9 10
示例:
for (var i = 0; i < 10; i++){ // do something }
console.log(i); 输出: 10
很明显,我们不希望i,这个变量变为全局变量。
let 示例代码
1 2 3 4 5 6 7 8 9 10
'use strict' { var b=1; let a=2; }
console.log(a); console.log(b);
# 输出: ReferenceError: a is not defined
上一节我们给出了如下的示例:
1 2 3 4 5 6 7 8 9 10 11
var a = []; for (var i = 0; i < 10; i++) { a[i] = function () { console.log(i); }; } a[1](); a[2](); a[3]();