src/Db/Orm/ActiveQuery.php
public function prepareAsync($builder)
    {
        // NOTE: because the same ActiveQuery may be used to build different SQL statements
        // (e.g. by ActiveDataProvider, one for count query, the other for row data query,
        // it is important to make sure the same ActiveQuery can be used to build SQL statements
        // multiple times.
        if (!empty($this->joinWith)) {
            $this->buildJoinWith();
            $this->joinWith = null; // clean it up to avoid issue https://github.com/yiisoft/yii2/issues/2687
        }

        if (empty($this->from)) {
            $this->from = [$this->getPrimaryTableName()];
        }

        if (empty($this->select) && !empty($this->join)) {
            list(, $alias) = $this->getTableNameAndAlias();
            $this->select = ["$alias.*"];
        }

        if ($this->primaryModel === null) {
            // eager loading
            $queryPr = new LazyPromise(function() {
                return resolve(Query::create($this));
            });
        } else {
            // lazy loading of a relation
            $queryPr = $this->prepareAsyncVia();
        }

        return $queryPr->thenLazy(function($query) {
            /** @var QueryInterface $query */
            if (!empty($this->on)) {
                $query->andWhere($this->on);
            }
            return $query;
        });
    }
public function prepareAsync($builder)
    {
        // NOTE: because the same ActiveQuery may be used to build different SQL statements
        // (e.g. by ActiveDataProvider, one for count query, the other for row data query,
        // it is important to make sure the same ActiveQuery can be used to build SQL statements
        // multiple times.
        if (!empty($this->joinWith)) {
            $this->buildJoinWith();
            $this->joinWith = null; // clean it up to avoid issue https://github.com/yiisoft/yii2/issues/2687
        }

        if (empty($this->from)) {
            $this->from = [$this->getPrimaryTableName()];
        }

        if (empty($this->select) && !empty($this->join)) {
            list(, $alias) = $this->getTableNameAndAlias();
            $this->select = ["$alias.*"];
        }

        if ($this->primaryModel === null) {
            // eager loading
            $queryPr = new LazyPromise(function() {
                return resolve(Query::create($this));
            });
        } else {
            // lazy loading of a relation
            $queryPr = $this->prepareAsyncVia();
        }

        return $queryPr->thenLazy(function($query) {
            /** @var QueryInterface $query */
            if (!empty($this->on)) {
                $query->andWhere($this->on);
            }
            return $query;
        });
    }
{@inheritdoc}