如何在调用下一个函数之前等待函数完成? [英] How can one wait for a function to complete before the next function is called?

查看:176
本文介绍了如何在调用下一个函数之前等待函数完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用下一个函数之前等待函数完成的最佳方法是什么?

What is the best way to wait for a function to complete before the next function is called?

我有一个连续调用两个函数的场景,一个在另一个。我需要在调用第二个之前完成第一个。

I have a scenario where two functions are called consecutively, one after the other. I need the first one to complete before the second one is called.

Function1();   
Function2();   

如何判断代码等待 Function1 在尝试 Function2 之前完成?

How do I tell the code to wait for Function1 to complete before attempting Function2?

WebDevelopment:解决方案可以是javascript或jQuery,也可以是基于Ajax的。

WebDevelopment: solution can be javascript or jQuery, or Ajax based.

谢谢......

推荐答案

您可以使用 Promise 回调解决这个问题,Promise是更现代,更清洁的做事方式:

You could either use Promise or callback to solve this problem, with Promises being the more modern and cleaner way of doing things:

承诺:

function foo() {
   return new Promise(function(resolve, reject) {
      // do some work
      resolve(optionalParam);
      // something caused an error whoops
      reject(optionalParam);
   })
}

function bar() {
    // do something
};

// Call bar only once foo finishes
foo().then(function() {
   bar();
}, function() {
   // this is the reject case an error here
});

Promise可以被链接,这意味着如果bar是Promise,我们可以链接另一个然后事件。

Promises are able to be chained, meaning that if bar was a Promise we could chain another then event to that.

你也可以使用回调来解决你的问题

You could also use callbacks to solve your problem

function foo(cb) {
   var data = 'alex';
   cb(data);
}

function bar(name) {
   // some processing
   console.log(name);
}

// Call foo, then execute bar when foo finishes
foo(function(data) {
    bar(data);  // alex would be logged once the callback fires
});

在这里的回调示例中,我们将一个函数作为参数传递,一旦运行的功能块遇到你的调用cb参数,它将调用模拟异步的函数。请记住它不会停止该函数的执行上下文。如果我们有这样的代码:

In the callback example here, we pass a function as a parameter, once the running function block encounters your invocation of the cb parameter, it will invoke the function which emulates the asynchronousy. Just remember though that it does not stop the execution context on that function. If we had code like:

function foo(cb) {
    var data = 'alex';
    cb(data);
    console.log('this is after so don't print it'); <-- still prints
};

最后的 console.log 将在何时打印回调在事件队列上完成它的时间(当然,除非您在CB中触发一个偶数,就像一个http请求,其中日志将被触发,然后http请求将在之后完成)。

The final console.log will print when the callback finishes its time on the event queue (unless of course you fire an even in CB like a http request, in which the log will fire and then the http request would finish after).

要停止上一个控制台日志,你需要明确地告诉javascript在调用cb时返回,以防止在该方法中进一步执行,并且一旦你解决或拒绝执行的承诺,我个人就会赞成Promises该函数范围内的上下文已停止。

To stop the last console log you would need to explicitly tell javascript to return when cb is called to prevent further execution in that method, and personally this is where I favour Promises, once you resolve or reject the promise the execution context within that function scope is stopped.

这篇关于如何在调用下一个函数之前等待函数完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆