Skip to content

Commit

Permalink
chore(Ch2): promised-mapping から timer-promisefyへと実装を変更
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Jun 13, 2014
1 parent fdb94ba commit bdb929f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 9 deletions.
File renamed without changes.
10 changes: 10 additions & 0 deletions Ch2_HowToWrite/lib/timer-promisefy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
// `delay`ミリ秒後にresolveされるpromiseを返す
function timerPromisefy(delay) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve(delay);
}, delay);
});
}
module.exports.timerPromisefy = timerPromisefy;
11 changes: 8 additions & 3 deletions Ch2_HowToWrite/src/promise-all-timer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"use strict";
var promisedMapping = require("./promised-mapping");
var promisedMap = promisedMapping([1, 2, 4, 8, 16, 32]);
var timerPromisefy = require("../lib/timer-promisefy").timerPromisefy;
var startDate = Date.now();
Promise.all(promisedMap).then(function (values) {
// 全てがresolveされたら終了
Promise.all([
timerPromisefy(1),
timerPromisefy(32),
timerPromisefy(64),
timerPromisefy(128)
]).then(function (values) {
console.log(Date.now() - startDate + "ms");// 約32ms
console.log(values); // [1, 2, 4, 8, 16, 32]
});
12 changes: 8 additions & 4 deletions Ch2_HowToWrite/src/promise-race-timer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"use strict";
var promisedMapping = require("./promised-mapping");
var promisedMap = promisedMapping([1, 32, 64, 128]);
// 一番最初のものがresolveされた時点で終了
Promise.race(promisedMap).then(function (value) {
var timerPromisefy = require("../lib/timer-promisefy").timerPromisefy;
// 一つでもresolve または reject された時点で終了
Promise.race([
timerPromisefy(1),
timerPromisefy(32),
timerPromisefy(64),
timerPromisefy(128)
]).then(function (value) {
console.log(value); // => 1
});

Expand Down
2 changes: 1 addition & 1 deletion Ch2_HowToWrite/test/promised-mapping-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
"use strict";
var assert = require("power-assert");
var promisedMapping = require("../src/promised-mapping");
var promisedMapping = require("../experiments/promised-mapping");
describe("#promisedMapping", function () {
context("When passing []", function () {
it("should return []", function () {
Expand Down
26 changes: 26 additions & 0 deletions Ch2_HowToWrite/test/timer-promisefy-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";
var assert = require("power-assert");
var timerPromisefy = require("../lib/timer-promisefy").timerPromisefy;

function isPromise(obj) {
return obj && typeof obj.then === 'function';
}
describe("timer-promisefy", function () {
it("should return promise obejct", function () {
assert(isPromise(timerPromisefy(1)));
});
it("resolve with arged value", function () {
var time = 2;
return shouldFulfilled(timerPromisefy(time)).then(function (value) {
assert(value === time);
});
});
it("resolve after arg ms", function () {
var time = 2;
var limit = time + 100;
var now = Date.now();
return shouldFulfilled(timerPromisefy(time)).then(function (value) {
assert(Date.now() - now < limit);
});
});
});
2 changes: 1 addition & 1 deletion Ch4_AdvancedPromises/race-delay-timeout.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Promise.raceを使ったタイムアウトの実装を学んでいきます。

まずはタイムアウトをPromiseでどう実現するかを見て行きたいと思います。

タイムアウトというのは一定時間経ったら何かするという処理なので、`setTimtout`を使えばいいことが分かりますね。
タイムアウトというのは一定時間経ったら何かするという処理なので、`setTimeout`を使えばいいことが分かりますね。

まずは単純に`setTimeout`をPromiseでラップした関数を作ってみましょう。

Expand Down

0 comments on commit bdb929f

Please sign in to comment.