鈍足ランナーのIT日記

走るのが好きな5流のITエンジニアのブログ。

趣味の範囲は広いけど、どれも中途半端なクソブロガー楽しめるWebアプリを作ってあっと言わせたい。サーバーサイドPerl(Mojolicious)、クライアントサイドVue.js。Arduinoにも触手を伸ばす予定。

Promiseを学ぼう

コールバック地獄はちょつとつらいので ニフティクラウドさんでも使えるPromiseを学ぼう。 じっくり読もう。

NCMBhttp://mb.cloud.nifty.com/ でPromiseを使ってみる。 resolveがasになっていて、 allがwhenになっているようだった。

var promise = NCMB.Promise.when([NCMB.Promise.as(1),NCMB.Promise.as(2)]);
promise.then(
    function(ret1,ret2){
        console.log(ret1);
        console.log(ret2);
        return NCMB.Promise.when([NCMB.Promise.as(3),NCMB.Promise.as(4)]);
    }
).then(function (ret1,ret2){
    console.log(ret1);
    console.log(ret2);
});

でも、これだと引数の数が2つと決め打ちなんだけど。可変配列をどう受けるんだろう。

NCMBhttp://mb.cloud.nifty.com/ のコミュニティにイシュー投げて回答もらいました!

var promise = NCMB.Promise.when([NCMB.Promise.as(1),NCMB.Promise.as(2)]);
promise.then(
    function(ret1,ret2){
        console.log(ret1);
        console.log(ret2);
        return NCMB.Promise.when([NCMB.Promise.as(3),NCMB.Promise.as(4)]);
    }
).then(function (){
    console.log(arguments[0]);
    console.log(arguments[1]);
});