How can I make this call to request in nodejs synchronous?

后端 未结 6 1730
旧时难觅i
旧时难觅i 2020-11-30 05:07

I have a function in my nodejs application called get_source_at. It takes a uri as an argument and its purpose is to return the source code from that uri. My problem is that

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 05:48

    You can with deasync:

    function get_source_at(uri){
        var source;
        request({ uri:uri}, function (error, response, body) {
            source = body;
            console.log(body);
        });
        while(source === undefined) {
          require('deasync').runLoopOnce();
        }
        return source;
    }
    

提交回复
热议问题