简体   繁体   English

等待异步任务完成

[英]Wait for async task to finish

I'm interacting with a third-party JavaScript library where some function calls are asynchronous.我正在与第三方 JavaScript 库交互,其中一些 function 调用是异步的。 Instead of working the asynchronous logic into my application, I preferred to write synchronous wrappers to those async calls.我没有将异步逻辑应用到我的应用程序中,而是更喜欢为那些异步调用编写同步包装器。 I know, I know, it's terrible design, but this is a demo project with very high chance of being rewritten entirely.我知道,我知道,这是糟糕的设计,但这是一个很有可能被完全重写的演示项目。 I need something to show the team the concept, not really having to worry performance, yet.我需要一些东西来向团队展示这个概念,而不必真正担心性能。

Here's what I wanna do:这是我想做的:

function sync_call(input) {
    var value;

    // Assume the async call always succeed
    async_call(input, function(result) {value = result;} );

    return value;
}

I tried the jQuery's deferred and promise but it seems to be aiming at the async design pattern.我尝试了 jQuery 的 deferred 和 promise 但它似乎是针对异步设计模式的。 I want to use the synchronous pattern in my code.我想在我的代码中使用同步模式。

This will never work , because the JS VM has moved on from that async_call and returned the value, which you haven't set yet. 这将永远不会起作用 ,因为JS VM已经从async_call继续并返回了您尚未设置的值。

Don't try to fight what is natural and built-in the language behaviour. 不要试图打击自然和内置的语言行为。 You should use a callback technique or a promise. 您应该使用回调技术或承诺。

function f(input, callback) {
    var value;

    // Assume the async call always succeed
    async_call(input, function(result) { callback(result) };

}

The other option is to use a promise, have a look at Q . 另一种选择是使用承诺,看看Q. This way you return a promise, and then you attach a then listener to it, which is basically the same as a callback. 这样你就可以返回一个promise,然后你将一个then侦听器附加到它,这与回调基本相同。 When the promise resolves, the then will trigger. 当promise解决后,将触发。

How about calling a function from within your callback instead of returning a value in sync_call()? 如何在回调中调用函数而不是在sync_call()中返回值?

function sync_call(input) {
    var value;

    // Assume the async call always succeed
    async_call(input, function(result) {
        value = result;
        use_value(value);
    } );
}

Here is a working example of how:-这是一个如何工作的示例:-

function testAsync(){
    return new Promise((resolve,reject)=>{
        //here our function should be implemented 
        setTimeout(()=>{
            console.log("Hello from inside the testAsync function");
            resolve();
        ;} , 5000
        );
    });
}

async function callerFun(){
    console.log("Caller");
    await testAsync();
    console.log("After waiting");
}

callerFun();

Outputs:输出:

Caller
Hello from inside the testAsync function
After waiting

To make it more complete, error handling should be added (deal with the reject() case).为了使其更完整,应该添加错误处理(处理 reject() 情况)。

See here for other examples: https://www.delftstack.com/howto/javascript/javascript-wait-for-function-to-finish/有关其他示例,请参见此处: https://www.delftstack.com/howto/javascript/javascript-wait-for-function-to-finish/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM