Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 异步/等待链接何时停止?_Javascript_Node.js_Asynchronous_Async Await_Node Fetch - Fatal编程技术网

Javascript 异步/等待链接何时停止?

Javascript 异步/等待链接何时停止?,javascript,node.js,asynchronous,async-await,node-fetch,Javascript,Node.js,Asynchronous,Async Await,Node Fetch,我正在测试节点获取,当我使用async/await时,出现了一个问题:如果我在函数中使用await,我必须使函数异步,但由于我的函数是异步的,我需要等待它并使父函数异步。等等背景: async functionA() { var result = await functionB(); } async functionB() { //do things var result = await functionC(); //do things var blob; //d

我正在测试节点获取,当我使用async/await时,出现了一个问题:如果我在函数中使用await,我必须使函数异步,但由于我的函数是异步的,我需要等待它并使父函数异步。等等背景:

async functionA()
{
  var result = await functionB();
}

async functionB()
{
  //do things
  var result = await functionC();

  //do things

  var blob;

  //do things

  return blop;
}

async functionC()
{
 //here is the real async call
 var result = await fetch(....);
 var json = await result.json();
 return json;
}
}

如何使此异步/等待链接停止?我的程序中有一个使用fetch的方法,它将使我将所有其他方法转换为异步方法。这真的是用fetch开发的所有其他程序的样子吗?也许我不理解async/await的某些内容


感谢您的时间:

在脚本开始时调用异步函数一次。在它的响应返回后,用结果同步调用脚本的其余部分。大致如下:

async function functionC() {
 //here is the real async call
 const response = await fetch(....);
 const result = await result.json();
 return result;
}

functionC()
  .then((results) => {
    // do stuff with results
    functionA(results);
    functionB(results); // if needed
    // etc
  })
  .catch(handleErrors); // don't forget this part
这样,只有脚本的一部分(接近入口点)需要处理异步逻辑。实现这一点可能需要进行一些重构,但与使所有内容都异步相比,这是值得的


还请注意,.json可能解析为普通对象或数组-它不会解析为json格式的字符串,它会像解析json一样解析响应流,并解析为结果,结果不再是json格式的字符串,而是一个普通值,通常是一个对象或数组

调用异步函数一次,在脚本的开头。在它的响应返回后,用结果同步调用脚本的其余部分。大致如下:

async function functionC() {
 //here is the real async call
 const response = await fetch(....);
 const result = await result.json();
 return result;
}

functionC()
  .then((results) => {
    // do stuff with results
    functionA(results);
    functionB(results); // if needed
    // etc
  })
  .catch(handleErrors); // don't forget this part
这样,只有脚本的一部分(接近入口点)需要处理异步逻辑。实现这一点可能需要进行一些重构,但与使所有内容都异步相比,这是值得的


还请注意,.json可能解析为普通对象或数组-它不会解析为json格式的字符串,而是解析响应流,就像解析为json一样,并解析为结果,结果不再是json格式的字符串,而是普通值,通常是对象或数组

从非异步方法调用异步方法

简短答复:

int GetData(DateTime x)
{
    Task<int> taskGetData = Task.Run( () => GetDataAsync(x));
    taskGetData.Wait();
    return taskGetData.Result;
}

async Task<int> TaskGetDataAsync(DateTime x)
{
    ...
}
更详细的回答:当GetDataAsync等待数据写入文件或从数据库获取数据时,您的过程可以自由地执行其他操作

int GetData(DateTime x)
{
    Task<int> taskGetData = Task.Run( () => GetDataAsync(x));

    DoSomethingElse();

    taskGetData.Wait();
    int taskResult = taskGetData.Result;
    return taskResult;
}


    

从非异步方法调用异步方法

简短答复:

int GetData(DateTime x)
{
    Task<int> taskGetData = Task.Run( () => GetDataAsync(x));
    taskGetData.Wait();
    return taskGetData.Result;
}

async Task<int> TaskGetDataAsync(DateTime x)
{
    ...
}
更详细的回答:当GetDataAsync等待数据写入文件或从数据库获取数据时,您的过程可以自由地执行其他操作

int GetData(DateTime x)
{
    Task<int> taskGetData = Task.Run( () => GetDataAsync(x));

    DoSomethingElse();

    taskGetData.Wait();
    int taskResult = taskGetData.Result;
    return taskResult;
}


    

一旦你去异步你真的不能回去你不必使用等待。你可以用。然后是的。如果您在程序中执行异步操作,那么整个顶级程序都是异步的。但是,一旦你去异步你真的不能回去你不必使用等待。你可以用。然后是的。如果您在程序中执行异步操作,那么整个顶级程序都是异步的。但是