Created
April 29, 2016 07:48
-
-
Save RammusXu/69e8c2471b1cda4bb0cacbca823da3ef to your computer and use it in GitHub Desktop.
how to refactor it ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = [ | |
profileUtil.createUser('[email protected]', 'qwer1234', 'test', 'Company'), | |
profileUtil.createUser('[email protected]', 'qwer1234', 'test2', 'Company'), | |
]; | |
request[0].then(function(value) { | |
return request[1]; | |
}) | |
.then(function(value) { | |
return request[2]; | |
}) | |
.then(function (value) { | |
done(); | |
}) | |
.catch(function(reason) { | |
console.log(reason); | |
// (reason.errmsg).should.include('duplica2te'); | |
done(); | |
}); |
By the way,
如果for loop那邊用的i是用let宣告的(不是var),應該就不會有這問題了
node要用es6語法的話,開頭加'use strict'
就可以用const, let宣告,可以避免很多var常見的陷阱(hoisting)
另外也支援這樣的匿名function
//1 原本宣告方法
let f1 = function(arg1,arg2) {
return arg1+arg2;
}
//2 單行,不用return跟分號
let f2 = (arg1, arg2)=> arg1+arg2
//3 多行,要自己加return
let f3 = (arg1, arg2)=> {
return arg1+arg2;
}
/*
其他還有一些list的operation
像是filter, map
*/
let arr = [1,2,3];
//[2,4,6]
console.log(arr.map(value=>value*2));
//[2,3]
console.log(arr.filter(value=>value>=2));
//[1,2,3]
console.log(arr);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
或是把job包成function 需要的時候再執行()