Created
April 15, 2018 15:28
-
-
Save shisama/021e5e1f1b1f0f9183e492ad40456532 to your computer and use it in GitHub Desktop.
V8 6.3で追加されたECMAScriptの機能 (Dynamic import、Asynchronous Iteration、 Promise.prototype.finally) ref: https://qiita.com/shisama/items/f7029822a5848592cbc2
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
if (member.isLoggedIn()) { | |
// ログインしている場合はmoduleAの関数を使いたい | |
} else { | |
// ログインしていない場合はmoduleBの関数を使いたい | |
} |
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
import moduleA from './moduleA'; | |
import moduleB from './moduleB'; | |
if (member.isLoggedIn()) { | |
// ログインしている場合はmoduleAの関数を使いたい | |
moduleA.func(); | |
} else { | |
// ログインしていない場合はmoduleBの関数を使いたい | |
moduleB.func(); | |
} |
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
promiseFunc() | |
.then(() => { | |
someFunction(); | |
closeFunction(); // 成功した場合用 | |
}) | |
.catch(err => { | |
console.log(err); | |
closeFunction(); // エラーになった場合用 | |
}); |
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
promiseFunc() | |
.then(() => { | |
someFunction(); | |
}) | |
.catch(err => { | |
console.log(err); | |
}) | |
.finally(() => { | |
closeFunction(); // 必ず実行される | |
}); |
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
(async() => { | |
try { | |
await promiseFunc(); | |
await someFunction(); | |
} catch (err) { | |
console.log(err); | |
} finally { | |
closeFunction(); | |
} | |
})(); |
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
Promise.resolve("resolve") | |
.then(val => {console.log(val);return "then"}) | |
.then(val => {console.log(val);throw new Error("catch")}) | |
.catch(err => {console.log(err.message)}) | |
.finally(() => {console.log("finally")}); |
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
if (member.isLoggedIn()) { | |
// ログインしている場合はmoduleAの関数を使いたい | |
import('./moduleA.js') // ここでmoduleAが読み込まれる | |
.then(module => { | |
module.func(); | |
}); | |
} else { | |
// ログインしていない場合はmoduleBの関数を使いたい | |
import('./moduleB.js') // ここでmoduleBが読み込まれる | |
.then(module => { | |
module.func(); | |
}); | |
} |
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
for (let i = 0; i < MAX; i++) { | |
import(`module${i}.js`) | |
.then(module => { | |
module.func(); | |
}); | |
} |
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
promiseFunc() | |
.then(res => { | |
return import('./module.js'); | |
}) | |
.then(module => { | |
module.func(data); | |
}); |
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
(async() => { | |
if (member.isLoggedIn()) { | |
// ログインしている場合はmoduleAの関数を使いたい | |
const module = await import('./moduleA.js') // ここでmoduleAが読み込まれる | |
module.func(); | |
} else { | |
// ログインしていない場合はmoduleBの関数を使いたい | |
const module = await import('./moduleB.js') // ここでmoduleBが読み込まれる | |
module.func(); | |
} | |
})() |
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
(async() => { | |
let count = 0; | |
// 1秒ごとにhello1、hello2、hello3をランダムで読み込む | |
let id = setInterval(async() => { | |
const i = Math.floor(Math.random() * 3) + 1; | |
const module = await import(`./import_modules/hello${i}.js`); | |
module.hello(); | |
count++; | |
if (count === 10) { | |
clearInterval(id); | |
} | |
}, 1000); | |
})(); |
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
(async() => { | |
const dataList = await Promise.all([ | |
fetch('https://qiita.com/api/v2/tags/Node.js'), | |
fetch('https://qiita.com/api/v2/tags/JavaScript'), | |
fetch('https://qiita.com/api/v2/tags/npm'), | |
]); | |
for (const data of dataList) { | |
console.log(data); | |
} | |
})(); |
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
(async() => { | |
const promises = []; | |
for (const tag of ["Node.js", "JavaScript", "npm"]) { | |
promises.push(fetch(`https://qiita.com/api/v2/tags/${tag}`)); | |
} | |
for await (const data of promises) { | |
console.log(data) | |
} | |
})(); |
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
/* | |
* 乱数を取得 | |
*/ | |
async function* gen() { | |
while (true) { | |
const res = await axios.get('https://www.random.org/decimal-fractions/?num=1&dec=10&col=1&format=plain&rnd=new'); | |
const num = res.data; | |
yield Number(num); | |
} | |
} | |
(async() => { | |
const BREAK = 0.8; | |
for await (const num of gen()) { | |
console.log(num); | |
if (num > BREAK) { | |
console.log("over", BREAK); | |
break; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment