LoginSignup
10
6

More than 5 years have passed since last update.

Async/Await + 同期関数の非同期関数化

Last updated at Posted at 2017-07-25

はじめに

Async/Awaitにより同期処理と非同期処理の垣根が低くなり、可読性の高いコードを実現できるようになりました。
しかしサードパーティのライブラリを眺めても、同期関数と非同期関数が全く同じような書き方で書ける方法はなかなか存在しません。そこで同期関数を非同期関数化することにより、Async/Awaitを用いてほぼ同じ書き方で可読性の高いコードを実現する方法を紹介します。

同期関数の非同期関数化

今回は同期処理でおなじみのLodashAigleを用いて非同期化します。
今回のゴールは、Lodashの関数を非同期化することにより、Lodashと同じ使い方で非同期処理をすることです。

今回使用するライブラリをインストールします。

$ npm install -S lodash aigle

次にLodashの関数を非同期化します。

const _ = require('lodash');
const Aigle = require('aigle');
Aigle.mixin(_);

以上で非同期化されました。

非同期関数のサンプルとしてdelayという関数を定義します。この関数はms後にresultを返す非同期関数です。

function delay(ms, result) {
  return new Promise(resolve => setTimeout(resolve, ms, result));
}

Lodash.map

Lodash.mapを非同期関数化した例です。Aigle.mapは10ms後にそれぞれのresultを返し、全てのarrayのresultが揃い次第結果を返します。

const array = [1, 2, 3];

const syncResult = _.map(array, n => n * 2); // [2, 4, 6];

const asyncResult = await Aigle.map(array, n => delay(10, n * 2)); // [2, 4, 6]

Lodash.find

Lodash.findを非同期関数化した例です。Aigle.findtruthyを受け取り次第結果を返します。

const array = [1, 2, 3];

const syncResult = _.find(array, n => n === 2); // 2

const asyncResult = await Aigle.find(array, n => delay(10, n === 2)); // 2

Lodash.chain

AigleではLodash.chainのようにメソッドチェインを使用することが可能です。メソッドチェインではLodash.chainと同様に直前の関数の結果を次の関数に渡します。

const array = [1.1, 1.4, 2.2];

const syncResult = _.chain(array)
  .map(n => n * 2) // [2.2, 2.8, 4.4]
  .uniqBy(Math.floor) // [2.2, 4.4]
  .sum() // [6.6]
  .times() // [0, 1, 2, 3, 4, 5]
  .value();

const asyncResult = await Aigle.resolve(array)
  .map(n => delay(10, n * 2)) // [2.2, 2.8, 4.4]
  .uniqBy(Math.floor) // [2.2, 4.4]
  .sum() // [6.6]
  .times(); // [0, 1, 2, 3, 4, 5]

またAigleの各関数は同期処理・非同期処理双方の処理が可能です。サンプルコード はこちらにも載せておきました。

まとめ

Aigle.mixinLodash以外の関数・ライブラリにも使用することができます。またAigleは可読性の高いコードを提供するだけでなく、高パフォーマンスのコードを実現しています。興味ある方は下記の記事も読んでいただけたら嬉しいです。すでに本番環境でも使用しています、是非使ってみてください。

リンク

10
6
2

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
10
6