-
-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Ch2): promised-mapping から timer-promisefyへと実装を変更
#101 (comment) fixes #123
- Loading branch information
Showing
7 changed files
with
54 additions
and
9 deletions.
There are no files selected for viewing
File renamed without changes.
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
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; |
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
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] | ||
}); |
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
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
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
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); | ||
}); | ||
}); | ||
}); |
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