Node.js async to sync

后端 未结 5 1025
走了就别回头了
走了就别回头了 2020-12-13 19:44

How can I make this work

var asyncToSync = syncFunc();

function syncFunc() {
    var sync = true;
    var data = null;
    query(params, function(result){
          


        
5条回答
  •  悲哀的现实
    2020-12-13 19:45

    The issue you are having is that your tight while loop is blocking. So I don't think your query callback will ever be run. I think you need to use setTimeout or the like to prevent the function from blocking, but should you do so, the function will return before the callback is called. This functionality must be implemented at a lower level.

    If you are in the browser, you might check out this article. In node you have to rely on the implementation of whatever you're querying. It may or may not provide synchronous methods.

提交回复
热议问题