标签: synchronous

在javascript中同步调用,但不处理同步

我遇到了同步调用的一些问题,似乎无法准确理解出现了什么问题.

在调试代码时,它会完美地填充songTracks数组,直到return语句再次为空.

代码示例:

function getAllSongIds(lijstId){
  var songTracks = [];
  $.ajax({
    url: "http://somehost.com/lists/"+lijstId+"/editions/",   
      dataType: "jsonp",
      async: false,
      success: function(json){

        for (i in json.editions) {
          $.ajax({
            url:"http://somehost.com/lists/"+lijstId+"/editions/"+json.editions[i].id,
            dataType:"jsonp",
            async: false,
            success: function(json2){
              for(j in json2.tracks){
                if(songTracks.indexOf(json2.tracks[j].id) === -1){
                  songTracks.push(json2.tracks[j].id);
                }
              }
            }
          })
        };
      }

    });
  alert(songTracks);
  return songTracks;
};
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery synchronous

0
推荐指数
1
解决办法
42
查看次数

奇怪的Javascript封闭错误

这是一些代码.

function callServiceSync(url, obj) {
    var result = true;
    callServiceOptions(url, obj, function(res){
        result = res;
        alert("RESULT CB: "+JSON.stringify(result));
    }, {async: false});
    alert("RESULT POST-CB: "+JSON.stringify(result));
    return result;
}
Run Code Online (Sandbox Code Playgroud)

运行时,警告框显示:

结果CB:{"成功":真实,"数据":"dtrombley"}

(这就是webservice实际返回的内容),然后:

结果POST-CB:是的

为什么闭包变量的赋值不起作用?我误解了JS闭包是如何工作的吗?

callServiceOptions()相当漫长 - 但它的要点是它调用jQuery的$ .ajax方法,它的最后一个参数选项扩展为某些默认值(在这种情况下,async禁用同步查询),然后执行提供的回调.

是$ .ajax()可能以某种方式执行某些禁用/搞砸闭包的方式(但称之为cb,而不是$ .ajax()!)?如果是这样,如何解决?

为了完整性(虽然这个功能真的不应该让我的想法搞砸了):

function callServiceOptions(url, obj, cb, options) {
    optSuccess = options.success;
    optError = options.error;
    opts = {}
    $.extend({},options)
    if (!opts.contentType) {
        opts.contentType = "application/json";
    }
    if (!opts.dataType) {
        opts.dataType = "json";
    }
    if (!opts.data && obj) {
        opts.data = JSON.stringify(obj); …
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery synchronous

0
推荐指数
1
解决办法
67
查看次数

什么是同步和异步方法?

什么是同步方法,什么是异步方法?同步和异步方法有什么区别?我什么时候必须使用同步或异步方法?我问这些问题是因为我不明白:

public async void ReadData(filepath)
{
    CreateDoc("hello");    //<------ Why I can't do that ?
}

public void CreateDoc(string astring)
{
    Debug.WriteLine(astring);
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能这样做?:

public async void ReadData(filepath)
{
     var BarreDroite = new string[] { "|" };
     foreach (string tableArret in items.Split(BarreDroite, StringSplitOptions.RemoveEmptyEntries))
     {
         listeArret.Add(tableArret); //<---- Here appear the problem.
     }
{
Run Code Online (Sandbox Code Playgroud)

我问这个问题是因为我在网上找不到明确的解释。

c# asynchronous synchronous

-1
推荐指数
2
解决办法
1万
查看次数

.hide("慢")是同步还是异步方法?

我们知道$.ajax()这是一个异步方法,beacuse next语句在ajax()方法完全执行之前开始执行,'ajax()'继续并行执行他的东西,并且hide()是一个Synchronous方法,因为它会立即隐藏元素,而下一个语句将在hide()真正完成时执行他的整个任务,但我真的很困惑hide("slow").它看起来像异步,但我读过,它在浏览器中设置了计时器,一切都自动发生(现在hide("slow")没有任何并行),所以在某种程度上,它也在下一个语句开始执行之前完成了它的整个任务,所以 hide("slow")似乎也是同步方法,

我对这种同步异步概念非常困惑

有人能帮我理解这个概念吗?

javascript jquery asynchronous synchronous

-1
推荐指数
1
解决办法
203
查看次数

JavaScript for 循环是如何执行的?

for 循环之后的代码在顶部的for 循环之前执行。Javascript是同步执行的,为什么先在for循环后执行?

function solution(A) {
  let diff = [];

  for (let i = 1; i < A.length; i++) {
    let sum1 = 0;
    let sum2 = 0;

    for (let j = 0; j < i - 1; j++) {
      sum1 += A[j];
      console.log('Why this is executing later? ' + sum1);
    }

    for (let k = i; k < A.length; k++) {
      sum2 += A[k];
    }

    console.log('Why this is executing frist? ' + sum1);
    diff.push(Math.abs(sum1 - sum2)); …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous synchronous execution

-1
推荐指数
1
解决办法
47
查看次数

查询完成后强制终止 q 会话

我试图在查询完成后强制终止(而不是关闭)aq 会话,以节省计算机上的资源。

目前它正在使用:

conn.sendAsync("exit 0")

问题是,如果我在它之后再次运行查询(尝试重新打开连接并运行另一个查询),它可能会失败,因为之前的连接仍会被终止asynchronous

因此,我尝试对synchronous查询执行相同的操作,但在尝试时:

conn.sendSync("exit 0")

我得到:

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
python-BaseException
Run Code Online (Sandbox Code Playgroud)

我可以指定一个超时,以便 q 会话在 10 秒后自动终止,或者也许还有另一种方法来强制终止 q 会话?

我的代码如下所示:

conn = qc.QConnection(host='localhost', port=12345, timeout=10000)
conn.open()
res = None
try:
    res = conn.sendSync(query, numpy_temporals=True)
except Exception as e:
    print(f'Error running {query}: {e}')
conn.sendSync("exit 0")
conn.close()
Run Code Online (Sandbox Code Playgroud)

python asynchronous synchronous kdb qpython

-1
推荐指数
2
解决办法
321
查看次数

如何找出Node.js的执行顺序?

我目前正在尝试测试一些Node.js代码。现在,我试图弄清楚为什么我的代码console.log()相对于代码执行顺序而言是不同的顺序。这与操作系统相关的Node.js函数调用有关。

我的脚本按顺序执行此操作:

//First
fs.readdir(){ //read from a directory all the available files
   console.log some stuff #a
}

//Second
fs.readFile(){ //read entire text file
   console.log some stuff #b
}

//Third
//create readline interface using fs.createReadStream(filedir) then

interface{ //read from text file line by line
   console.log some stuff #c
}
Run Code Online (Sandbox Code Playgroud)

函数a使用路径:__dirname +'/ text_file /'
函数b和c使用路径:__dirname,' /text_file/ text1.txt'

输出:

a
c
b
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么发生此命令吗?我不是操作系统方面的专家,也不是Node.js在后台发生的事情。如果我要依赖此命令,是否会使我的应用程序混乱?

javascript asynchronous synchronous fs node.js

-4
推荐指数
1
解决办法
1504
查看次数

标签 统计

synchronous ×7

asynchronous ×5

javascript ×5

jquery ×3

ajax ×2

c# ×1

execution ×1

fs ×1

kdb ×1

node.js ×1

python ×1

qpython ×1