var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3], numbers is still [1, 4, 9]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map2016年6月29日 星期三
[JS] Array.prototype.map
return: an array
2016年6月20日 星期一
2016年6月14日 星期二
[ECMAScript 6] Promise
JavaScript6 Promise =/= Node.js Promise
Promise -> has a metadata "then" array storing list of functions
until resolve, the promise object puts the "then" list of functions into the queue
( previously store in the promise metadata )
javascript run through the queue
Ex 1
=============================
console.log('a');
setTimeout(function bbb() {
console.log('bbb start');
setTimeout(function bbb2() {
console.log('bbb2 here');
}, 1000);
console.log('bbb end');
}, 1000);
console.log('c');
'abc'
=============================
Result:
a
c
abc
bbb start
bbb end
bbb2 here
Ex 2
=============================
console.log('a');
var bPromise = new Promise(resolve => {
setTimeout(function bbb() {
console.log('b');
resolve(123);
}, 1000);
});
setTimeout(function ddd() {
bPromise.then(function eee() {
console.log('e');
});
}, 7000);
bPromise.then(function ccc() {
console.log('c1');
});
bPromise.then(function ccc2() {
console.log('c2');
});
console.log('oh');
'abc'
=============================
Result:
a
oh
abc
b
c1
c2
e
Ex 3
=============================
console.log('a');
var bPromise = new Promise(function (resolve) {
console.log('b');
setTimeout(function bbb() {
console.log('bbb');
resolve(123);
}, 1000);
});
var thenPromise = bPromise.then(function ccc() {
console.log('ccc');
return new Promise(resolve => {
setTimeout(function eee() {
console.log('e');
resolve(123);
}, 1000);
});
});
var thenPromise1 = bPromise.then(function ccc1(param) {
console.log('ccc1');
return 345;
});
thenPromise.then(function ddd(param) {
console.log('ddd');
});
'abc'
=============================
Result:
a
b
abc
bbb
ccc
ccc1
e
ddd
Ex 4
=============================
//create a class http which has get method which returns instance of promise
function http(url) {
this.get = new Promise(function(resolve, reject) {
var client = new XMLHttpRequest();
console.log('here');
client.open('get', url);
client.send();
client.onload = function () { //If you observe carefully this is also registering a callback fn
if (this.status == 200) {
console.log('resolve');
resolve(this.response);
} else {
console.log('reject');
reject(this.statusText);
}
};
client.onerror = function () { reject(this.statusText); }
});
}
//using http module
var successCallBack = function(res) {
console.log(res);
}
new http('/admin').get.then(successCallBack);
============================
Promise -> has a metadata "then" array storing list of functions
until resolve, the promise object puts the "then" list of functions into the queue
( previously store in the promise metadata )
javascript run through the queue
Ex 1
=============================
console.log('a');
setTimeout(function bbb() {
console.log('bbb start');
setTimeout(function bbb2() {
console.log('bbb2 here');
}, 1000);
console.log('bbb end');
}, 1000);
console.log('c');
'abc'
=============================
Result:
a
c
abc
bbb start
bbb end
bbb2 here
Ex 2
=============================
console.log('a');
var bPromise = new Promise(resolve => {
setTimeout(function bbb() {
console.log('b');
resolve(123);
}, 1000);
});
setTimeout(function ddd() {
bPromise.then(function eee() {
console.log('e');
});
}, 7000);
bPromise.then(function ccc() {
console.log('c1');
});
bPromise.then(function ccc2() {
console.log('c2');
});
console.log('oh');
'abc'
=============================
Result:
a
oh
abc
b
c1
c2
e
Ex 3
=============================
console.log('a');
var bPromise = new Promise(function (resolve) {
console.log('b');
setTimeout(function bbb() {
console.log('bbb');
resolve(123);
}, 1000);
});
var thenPromise = bPromise.then(function ccc() {
console.log('ccc');
return new Promise(resolve => {
setTimeout(function eee() {
console.log('e');
resolve(123);
}, 1000);
});
});
var thenPromise1 = bPromise.then(function ccc1(param) {
console.log('ccc1');
return 345;
});
thenPromise.then(function ddd(param) {
console.log('ddd');
});
'abc'
=============================
Result:
a
b
abc
bbb
ccc
ccc1
e
ddd
Ex 4
=============================
//create a class http which has get method which returns instance of promise
function http(url) {
this.get = new Promise(function(resolve, reject) {
var client = new XMLHttpRequest();
console.log('here');
client.open('get', url);
client.send();
client.onload = function () { //If you observe carefully this is also registering a callback fn
if (this.status == 200) {
console.log('resolve');
resolve(this.response);
} else {
console.log('reject');
reject(this.statusText);
}
};
client.onerror = function () { reject(this.statusText); }
});
}
//using http module
var successCallBack = function(res) {
console.log(res);
}
new http('/admin').get.then(successCallBack);
============================
訂閱:
文章 (Atom)
[康復路] 試完又試
見了醫生,因為血色素又變低了,醫生想檢查是否「缺鐵」為成因,檢查又檢查…… 感覺有些麻煩……又抽血,又要留樣本…… 令我回想起當日入院的時光,因為一些原因,留樣本只需留一次,免卻留三次的麻煩;現在每天都要留一次,連續三天。每天早上就要跑醫院一趟再上班。 幸好也完成了。 第二次抽血...
-
裝水喉要用銅喉, 不要鐵喉, 熱水冷水都是。 裝煤氣喉因化學原理要用特製鐵喉, 不能用銅喉。如果是用煤氣熱水爐, 就要搵煤氣公司整。 用煤氣熱水爐拆要找煤氣公司, 清拆費$165公價, 安裝費一般無特別改位公價$475, 如煤氣公司check到有漏氣, 要換喉, 可在牆外...
-
Wow, 又一突破,自己換水龍頭 話說尋晚搭完飛機返到香港,第一時間拎曬行李喼d衫出黎洗,執好d野後,想沖埋涼一機過洗曬d衫,但唔知係咪去完旅行太興奮,開完個水龍頭之後點關都關唔到,感覺到裏面有d野斷左/鬆左/甩左咁,唯有沖完涼即刻放個花灑落水桶度,即刻拿拿淋去關水制。。。我...
-
8 January 2012 at 15:16 前言 話說12月左右, 考試前幾日開始左下面隻智慧齒異常的痛, D牙肉好腫, 所以去左POLY睇牙。之前E生都話過我隻智慧齒斜生, 頂住前面隻牙, 一來容易SIP食物, 2來可能影響前面隻牙會蛀, 所以叫我最好...
-
What is Interceptor? http://viralpatel.net/blogs/spring-mvc-interceptor-example/ Add autowired interceptors from Java Config http://stac...
-
Source: http://www.hket.com/eti/article/913472db-26f6-465a-b643-c5daaba82f4b-406134?category=green_news&source=print&printable=tru...