1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

javascriptのカリー化?

Last updated at Posted at 2017-04-11

#経緯
Promiseのcallbackの中で1行で書きたくなった。
理由はcallbackの中の処理がダラダラと続くことが
多くを外に出すことで見やすくなるかなと
思ったため。

####ソシャゲでアイテムをあげたりする処理を書く時

let array = [1, 2, 3];
let msg = 'プレゼント';
return Promise.forEach(array, function(itemId){
    let item = findItem(itemId); // itemを探す
    return giveItem(item, msg); // itemをあげる
});

カリー化?するといけそう

let array = [1, 2, 3];
let msg = 'プレゼント';

// itemIdを引数に取る関数を返すメソッド
let giveItemFunc = function(msg){
    return function(itemId){
        let item = findItem(itemId); // itemを探す
        return giveItem(item, msg); // itemをあげる
    }
}
return Promise.forEach(array, giveItemFunc(msg));

こうすると1行でかけました。
単なる自己満に近いですね。。。
他にこういうのあるとかあればお待ちしております!

1
2
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?