Last week I wrote about the yield return statement in c# and how it allows for deferred execution. In that post I explained how it powers LINQ and explained some non-obvious behaviors. In this week’s post I want to do the same thing but for Javascript. ES6 (ES2015) is becoming more and more mainstream, but in terms of usage I mostly see the more common arrow-functions or block-scoping (with let an
The developers behind Node.js released version 4.0 last week. The new version is a major update that includes significant improvements and many new language features. It is the first release to incorporate improvements from io.js, a community-driven fork that recently reintegrated with upstream Node.js. The io.js project emerged earlier this year when prominent Node.js contributors decided to go t
「Ruby の Enumerator でジェネレータを作ったり、遅延評価してみる」や「Python でジェネレータを作ったり、遅延評価してみる」の記事を ECMAScript 6 のジェネレータを使って記述するとどのようになるのか, 実際に試してみました. ECMAScript 6 の各ブラウザや処理系での実装状況の詳細はこちらを参照してください. 現時点でこの記事のコードがそのまま実行できる処理系はないと思われます. コードを Babel 等を用いて ECMAScript 5 のコードに変換するか (この場合ジェネレータを利用するために Polyfill が必要になります) arrow functions などの処理系が対応していない部分を書き換えて実行する必要があります. ジェネレータの基本 ECMAScript 6 で提案されているジェネレータについては「function* – Ja
This blog post is outdated. Please read chapter “Generators” in “Exploring ES6”. This blog post is part of a series on iteration in ES6: Iterables and iterators in ECMAScript 6 ES6 generators in depth Generators, a new feature of ECMAScript 6 [1], are functions that can be paused and resumed. This helps with many applications: iterators, asynchronous programming, etc. This blog post explains how g
(2015/10/21追記) 記事を書いてから半年経ちましたが、最近はRxの良さを理解したり、ES7のasync/awaitがbabelによって実用的になりつつあったり等、またもやベストプラクティスとはなんだったのか状態です。とはいえ、いまのところPromiseは非同期処理の土台であり続けそうですし、Generatorもasync/awaitへの足がかりとして知っておくことのメリットは大きいかと思いますので、引き続き公開させたままとさせて頂きます。 (追記ここまで) 数ある非同期処理のプラクティスを試してみて、だいたいこれが良いんじゃないかというパターンが固まったので書きます。効用はコールバック地獄からの脱出と結局非同期どれが良いの感の払拭。ES6寄りです。 前提知識 JavaScript Promiseの本 http://azu.github.io/promises-book/ ジェネレ
node --harmony-generators script.jsSlides: pag.forbesl.co.uk Sequence 1function count(n){ var res = [] for (var x = 0; x < n; x++) { res.push(x) } return res } for (var x of count(5)) { console.log(x) } Sequence 2function count(){ var res = [] for (var x = 0; true; x++) { res.push(x) } return res } for (var x of count()) { console.log(x) } Sequence 3function* count(){ for (var x = 0; true; x++) {
下のようなコードが、親しみをこめて「コールバック地獄」とか「死のピラミッド」とか呼ばれているのはご存じですよね。 doAsync1(function () { doAsync2(function () { doAsync3(function () { doAsync4(function () { }) }) }) この状態がコールバック地獄かどうかは、意見の分かれるところでしょう。ネストがいくら深くても全く問題がないコードもあるからです。非同期のコードでフロー管理ができないほど複雑になってしまった場合は悪夢ですが。自分のコードがどの程度”ひどい”状態に陥っているかを確認するには、こう自問してみてください。「 doAsync1 の前に doAsync2 が実行された場合、リファクタリングにどこまで労力がかけられるか」と。ここでのゴールはネストの階層を減らすことではなく、モジュール化された(も
One of the most exciting new features coming in JavaScript ES6 is a new breed of function, called a generator. The name is a little strange, but the behavior may seem a lot stranger at first glance. This article aims to explain the basics of how they work, and build you up to understanding why they are so powerful for the future of JS. Run-To-Completion The first thing to observe as we talk about
はじめに ES6以降で使えるようになったgeneratorでJavaScriptの非同期処理を書く、という話をします。同様のトピックについてはすでに多くのブログで言及されているのですが、各所で異なった使われ方や解説がなされていることが多いように思われたので、ここではそれぞれの関係を明らかにしつつ、generatorのさまざまな使い方を整理するのが目的です。 ふつうのやり方 もっとも一般的なコールバックスタイルで書くとこうなる。コールバックの第1引数にErrorオブジェクト、第2引数に得られた結果を渡すのはnodeの慣習(エラー処理については今回は省略)。 function sleep(cb) { setTimeout(function() { cb(null, 'I woke up at ' + new Date()); }, 1000); } sleep(function(err, re
Two ECMAScript 6 [1] features enable an intriguing new style of asynchronous JavaScript code: promises [2] and generators [3]. This blog post explains this new style and presents a way of using it without promises. Overview # Normally, you make a function calls like this: let result = func(···); console.log(result); It would be great if this style of invocation also worked for functions that perf
Google グループでは、オンライン フォーラムやメール ベースのグループを作成したり、こうしたフォーラムやグループに参加したりすることで、大勢のユーザーと情報の共有やディスカッションを行うことができます。
Several months have passed since support for generator functions and the yield keyword arrived in Node.js v0.11.2. This news was greeted with great excitement, because generator syntax provides a much cleaner alternative to using callbacks when writing asynchronous server-side code. One of the biggest benefits of using JavaScript on the server is that you can (in theory at least) run the very same
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く