promiseãé çªã«å®è¡ãã
JavaScript の Promise を返す関数を直列で実行したいので Pinscher というライブラリを作ってみた。 - (define -ayalog '())ã«ã¤ãã¦ã ä¾ãã°ãéåæã«å®è¡ãããPromise p1, p2, p3ããã£ãã¨ãã¦callbackãp1, p2, p3ã®é çªã«å®è¡ãããå ´åãreduceã使ããã
ä¾1ã¯ãp1, p2, p3ã並åã«å®è¡ããä¸ã§ãcallbackã®é åºãä¿è¨¼ãããå ´åã
ã¾ããã¹ãç¨ã«ãä¸å®æéã¾ã£ã¦ããvalueã§resolveããé¢æ°resolveLaterãå®ç¾©ãã¦ããã
function resolveLater(value, ms) { return new Promise(function (resolve) { setTimeout(function () { resolve(value); }, ms); }); }
var promises = [ resolveLater('a', 1000), resolveLater('b', 1000), resolveLater('c', 2000), resolveLater('d', 0), ]; var callback = console.log.bind(console); promises.reduce(function (current, next) { var p = current.then(function (v) { return next; }); p.then(callback); return p; }, Promise.resolve()); // a, b, c, d
ãã®ããã«ãªããã¿ã¹ã¯ãã®ãã®ã¯ä¸¦åã«å®è¡ãããããcallbackã¯é çªã«å®è¡ãããã
éã«ãp1, p2, p3èªä½ãé çªã«å®è¡ãããå ´åãã¤ã¾ããããã¿ã¤ãã³ã°ã§åæã«åãã¦ãpromiseã1ã¤ã§ããå¿ è¦ãããå ´åãï¼ãããã¯ã¼ã¯ã¢ã¯ã»ã¹ãªã©ã§ãè² è·ããããããªãå ´åãªã©ã¯ãã£ã¡ã®ãã¿ã¼ã³ã«è¿ãå®è£ ããããã¨ã«ãªãï¼
var argList = [ ['a', 1000], ['b', 1000], ['c', 2000], ['d', 0] ]; var callback = console.log.bind(console); argList.reduce(function (current, args) { return current.then(function () { return resolveLater.apply(null, args).then(function (v) { callback(v); }); }); }, Promise.resolve());
éåæã³ã¼ãã®ãã¹ãã«ã¤ãã¦ã ããJest | Painless JavaScript Unit Testingãããããã§ãjestã使ãã¨ãsetTimeoutç³»ã®é¢æ°èªä½ãä¹ã£åã£ã¦ï¼å®è¡é åºãä¿è¨¼ããªããï¼åæçã«å®è¡ããªããsetTimeout.mock.callsã«å¼ã³åºãããæã®mocké¢æ°ã®å¼æ°ãè¨é²ãã¦ãã£ã¦ãããã®ã§ãå®éã®éåæã§ä¸ç¢ºå®ãªåä½ã«ä¾åããã«ãã¹ãããããã³ã¼ããæ¸ããã
ãã éåæã®ãã¹ãã¯ã©ã¡ãã«ããè¤éã«ãªããã¡ã§ãããç¨åº¦ã®ã¨ããã§è«¦ãã¦ãã¹ãã±ã¼ã¹ã¯æ¸ãããã§ãã¯ã¯æåãã¨ãã風ã«ããã®ãç¾å®çãªã®ãããããªãã
追è¨
ã³ã¡ã³ãã§ã©ã¤ãã©ãªã§è§£æ±ºãã¦ãããã¨ãå®ç¾ã§ãã¦ããªãã¨ææãããã®ã§ããã®ä»ã®ä¾ã«ã¤ãã¦è§¦ããã
ã¾ããä¸è¨ã³ã¼ãã«ã¯ãªãã£ããé çªã«å®è¡ãããpromiseãã³ã¼ãä¸ã§åçã«è¿½å ãããå ´åã åã ã®ã¿ã¹ã¯èªä½ã¯ä¸¦åã«å®è¡ãããåºåãé çªã«ãããå ´å
var callback = console.log.bind(console) var task = Promise.resolve(); var identity = Object; // ãã®å ´åfunction (a) { return a; } task = task.then(identity.bind(null, resolveLater('a', 1000))).then(callback); task = task.then(identity.bind(null, resolveLater('b', 1000))).then(callback); task = task.then(identity.bind(null, resolveLater('c', 2000))).then(callback); task = task.then(identity.bind(null, resolveLater('d', 0))).then(callback);
ãã®ããã«reduceãæåã§å±éãããã¨ã§åçã«è¿½å ã§ããããã«ãªãã
ããä¸ã¤ã®ä¾ã¨ãã¦ãåã ã®ã¿ã¹ã¯ã®å®è¡ãé çªã«ï¼åæã«ä¸ã¤ããåãããªãããã«ï¼ãããå ´å
var callback = console.log.bind(console) var task = Promise.resolve(); task = task.then(resolveLater.bind(null, 'a', 1000)).then(callback); task = task.then(resolveLater.bind(null, 'b', 1000)).then(callback); task = task.then(resolveLater.bind(null, 'c', 2000)).then(callback); task = task.then(resolveLater.bind(null, 'c', 0)).then(callback);
ãã®ããã«ãªãã Promiseã®ä¸ã«ã³ã³ããã¼ã«ããã¼ãå®è£ ããã©ã¤ãã©ãªã使ãããããã®ããã«ç´æ¥promiseãçµã¿åãããææ³ã使ã£ãã»ããããã¨æãçç±ã¯è¤æ°ãã£ã¦ãã¾ããèªç±åº¦ã®ç¹ã ä¾ãã°ããã«è¼ããã³ã¼ãã§ã¯ãå ¨ä½ã®ç¶æ ãPromiseã¨ãã¦è¡¨ç¾ããã¦ãã¦åãåºããã¨ãã§ããï¼reduceãç´æ¥ä½¿ã£ãä¾ã§ã¯ãreduceãè¿ãPromiseãããã«ãããï¼ã®ã§ãè¤æ°ã®ãã¿ã¼ã³ãçµã¿åããããã¨ãã§ããã å ·ä½çã«ã¯ãæåã®promiseæ°åãarrayã¨ãã¦å ¥ãã¦ãããåçã«å®è¡ããããã®ãå¾ãã追å ãããå ´åã
var identity = Object; // ãã®å ´åfunction (a) { return a; } var callback = console.log.bind(console); var task = [ resolveLater('a', 1000), resolveLater('b', 1000), resolveLater('c', 2000), resolveLater('d', 0), ].reduce(function (current, next) { var p = current.then(function (v) { return next; }); p.then(callback); return p; }, Promise.resolve()); task = task.then(identity.bind(null, resolveLater('e', 1000))).then(callback);
ãã®ããã«ã§ããã
reduceç¸å½ã®å¦çããã¦ããªãã³ã¼ãã®ããä¸ã¤ã®åé¡ã¯ãã¨ã©ã¼å¦çãåçã«ã¿ã¹ã¯ã追å ããä¾ã§ã¯å ¨ä½ã®ã¨ã©ã¼ã¨ããæ¦å¿µã¯èããããããªãããä¾ãã°ãè¨äºä¸ã®ã³ã³ããã¼ã«ããã¼ã©ã¤ãã©ãªã§ã¯ããã¹ã¦ã®å®è¡ãçµãã£ãç¶æ ã¨ãããã®ãPromiseã¨ãã¦åãåºããªãããã£ã¦ãå ¨ä½ã®ã©ããã§ã¨ã©ã¼ããã£ãå ´åã«ããããcatchããé¢æ°ãã¨ãããã®ãæ¸ããªãã
å ¨ä½ã®å¦çãPromiseã¨ãã¦è¡¨ç¾ããã¦ããå ´åã¯
task.then(null, function (err) { console.error(err); });
ãªã©ãæå¾ã«è¿½å ãããã¨ã§ãã¨ã©ã¼å¦çãæ¡ãã¤ã¶ããããããã¥ããã¨ã©ã¼ãé²ããã