Skip to content

Instantly share code, notes, and snippets.

@shisama
Created April 15, 2018 15:28
Show Gist options
  • Save shisama/021e5e1f1b1f0f9183e492ad40456532 to your computer and use it in GitHub Desktop.
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
if (member.isLoggedIn()) {
// ログインしている場合はmoduleAの関数を使いたい
} else {
// ログインしていない場合はmoduleBの関数を使いたい
}
import moduleA from './moduleA';
import moduleB from './moduleB';
if (member.isLoggedIn()) {
// ログインしている場合はmoduleAの関数を使いたい
moduleA.func();
} else {
// ログインしていない場合はmoduleBの関数を使いたい
moduleB.func();
}
promiseFunc()
.then(() => {
someFunction();
closeFunction(); // 成功した場合用
})
.catch(err => {
console.log(err);
closeFunction(); // エラーになった場合用
});
promiseFunc()
.then(() => {
someFunction();
})
.catch(err => {
console.log(err);
})
.finally(() => {
closeFunction(); // 必ず実行される
});
(async() => {
try {
await promiseFunc();
await someFunction();
} catch (err) {
console.log(err);
} finally {
closeFunction();
}
})();
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")});
if (member.isLoggedIn()) {
// ログインしている場合はmoduleAの関数を使いたい
import('./moduleA.js') // ここでmoduleAが読み込まれる
.then(module => {
module.func();
});
} else {
// ログインしていない場合はmoduleBの関数を使いたい
import('./moduleB.js') // ここでmoduleBが読み込まれる
.then(module => {
module.func();
});
}
for (let i = 0; i < MAX; i++) {
import(`module${i}.js`)
.then(module => {
module.func();
});
}
promiseFunc()
.then(res => {
return import('./module.js');
})
.then(module => {
module.func(data);
});
(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();
}
})()
(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);
})();
(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);
}
})();
(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)
}
})();
/*
* 乱数を取得
*/
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