Last active
August 29, 2015 14:05
-
-
Save hell0again/eca54a29a633c01b10c0 to your computer and use it in GitHub Desktop.
promiseと読みやすさ
This file contains 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
// 1. thenまで一気に書くとthen節の中身が入れ子状に並ぶ。関数呼び出しが先頭に並ぶので読みやすいというか見慣れた形 | |
var filePath = "**/*.tpl"; | |
fetchTemplateFileList(filePath).then(function(fileList) { | |
return Promise.all(_.map(fileList, function(filePath) { | |
return createHtmlTextFromTemplateFile(filePath).then(function(htmlText) { | |
return createTemplateTableFromHtmlText(htmlText).then(function(templateTable) { | |
return createModuleTextFromTemplateTable(templateTable).then(function(moduleText) { | |
return writeModuleFromText(moduleText, getOutFilePath(filePath)).then(function(result) { | |
console.log("wrote "+ getOutFilePath(filePath)); | |
}); | |
}); | |
}); | |
}); | |
})); | |
}).then(function(r) { | |
console.log("all done"); | |
}); | |
This file contains 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
# 2. coffee script版 | |
filePath = "**/*.tpl" | |
fetchTemplateFileList(filePath).then (fileList) -> | |
Promise.all(_.map fileList, (filePath) -> | |
createHtmlTextFromTemplateFile(filePath).then (htmlText) -> | |
createTemplateTableFromHtmlText(htmlText).then (templateTable) -> | |
createModuleTextFromTemplateTable(templateTable).then (moduleText) -> | |
writeModuleFromText(moduleText, getOutFilePath filePath).then (result) -> | |
console.log "wrote #{getOutFilePath filePath}" | |
) | |
.then (r) -> | |
console.log "all done" |
This file contains 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
// 3. Promiseの変数を用意してthenでチェーン。入れ子は浅くなるが読みやすいかというと。。 | |
var filePath = "**/*.tpl"; | |
var p = fetchTemplateFileList(filePath); | |
p.then(function(fileList) { | |
return Promise.all(_.map(fileList, function(filePath) { | |
var p2 = createHtmlTextFromTemplateFile(filePath); | |
p2.then(function(htmlText) { | |
return createTemplateTableFromHtmlText(htmlText); | |
}).then(function(templateTable) { | |
return createModuleTextFromTemplateTable(templateTable); | |
}).then(function(moduleText) { | |
return writeModuleFromText(moduleText, getOutFilePath(filePath)); | |
}).then(function(result) { | |
console.log("wrote "+ getOutFilePath(filePath)); | |
}); | |
return p2; | |
})); | |
}).then(function(r) { | |
console.log("all done"); | |
}); |
This file contains 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
# 4. coffee script版 | |
filePath = "**/*.tpl" | |
p = fetchTemplateFileList filePath | |
p.then (fileList) -> | |
Promise.all(_.map fileList, (filePath) -> | |
p2 = createHtmlTextFromTemplateFile filePath | |
p2.then (htmlText) -> | |
createTemplateTableFromHtmlText htmlText | |
.then (templateTable) -> | |
createModuleTextFromTemplateTable templateTable | |
.then (moduleText) -> | |
writeModuleFromText moduleText, getOutFilePath filePath | |
.then (result) -> | |
console.log "wrote #{getOutFilePath filePath}" | |
p2 | |
) | |
.then (r) -> | |
console.log "all done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment