dongzong1866 2019-07-27 17:35
浏览 104

如何从我的函数返回我的响应then()?

I'm trying to get a response using callback. but the problem is i couldn't get the response from the callback because the response is null and I do not have any ideas how to fix that. If someone have any idea how to solve my problem please.

I tried to get a data from a array and it's work.

class Deferred
{

    const PENDING = 0;
    const RESOLVED = 1;
    const REJECTED = 2;

    private $status = self::PENDING;

    //Await require these option for check each state of request.
    protected $finished, $notify, $failed;

    protected $cancelled = false;

    protected $results = [];



    /**
     * Pool constructor.
     * @param callable|null $finished
     * @param callable|null $failed
     * @param callable|null $notify
     */
    public function __construct(?callable $finished = null, ?callable $failed = null, ?callable $notify = null)
    {
        $this->finished = $finished;
        $this->failed = $failed;
        $this->notify = $notify;
    }


    /**
     * @return int
     */
    public function getStatus(): int
    {
        return $this->status;
    }


    /**
     * @param $data
     * @return $this
     */
    public function resolve($data)
    {
        if ($this->getStatus() === self::PENDING && !$this->isCancelled()) {
            if (!is_null($this->finished)) {
                call_user_func($this->finished, $data);
            }
            $this->status = self::RESOLVED;
            $this->results = $data;
        }
        return $this;
    }


    /**
     * @param callable|null $finished
     * @param callable|null $failed
     * @param callable|null $notify
     */
    public function then(?callable $finished = null, ?callable $failed = null, ?callable $notify = null)
    {
        //response from these states
        if ($this->getStatus() != self::PENDING) {
            if (!is_null($finished) && $this->getStatus() == self::RESOLVED) call_user_func($finished, $this->results);
            if (!is_null($failed) && $this->getStatus() == self::REJECTED) call_user_func($failed, $this->results);           
        }
        if (!is_null($finished)) $this->finished = $finished;
        if (!is_null($notify)) $this->notify = $notify;
        if (!is_null($failed)) $this->failed = $failed;
    }
}

function getData(string $name) : bool{
$pool = new Deferred();
$my = MySQL::getData();
$result = $my->query("SELECT * FROM suggestions WHERE name = '" . $my->real_escape_string(strtolower($name)) . "'");
$response = '';
$pool->then(
    function($value) use(&$response) {
        $response = $value;

    }, function($value){
       echo "failed";

    });
    $pool->resolve($result);
if($response->num_rows > 0) return true;
        else return false;
}

var_dump(getData()); 

"Trying to get property 'num_rows' of non-object"

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀