javascript Javascript等待函数完成调用另一个内部函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53721653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Wait for function to complete the call another inside function
提问by Mayank
below is my function I am using "setTimeout(function()" for delay how can I wait the above function to complete and then execute the other function.
下面是我的函数我使用“setTimeout(function()”来延迟我如何等待上面的函数完成然后执行另一个函数。
function first_letter() {
var myArray = ["A", "D", "E", "F", "G", "H", "K", "M", "N", "O", "P", "R", "S", "T"];
var letter = myArray[Math.floor(Math.random() * myArray.length)];
bg_width = xx;
bg_height = xx;
load(xx, xx)
loading()
make(xxxxxxx)
setTimeout(function() {
fillText(xxxxxx)
}, 1500);
setTimeout(function() {
rounded(xxx)
}, 2000);
}
Like in given code. Wait until load(xx,xx) completes then executes make(xxxx).
就像在给定的代码中一样。等到 load(xx,xx) 完成然后执行 make(xxxx)。
回答by Luke Walker
There are many ways this can be achieved, although, its hard to see whats going on in your code example as some of the functions are out of scope. However, i have provided you with some examples below which i have assumed the contents of these functions.
有很多方法可以实现这一点,但由于某些功能超出范围,因此很难看出您的代码示例中发生了什么。但是,我在下面为您提供了一些示例,这些示例我已经假设了这些函数的内容。
You could try:
你可以试试:
Callback example
回调示例
Calling make()from within load(), by passing make()as a callback function.
make()从内部调用load(),通过make()作为callback function.
function load(xx, xx, callback){
/* load does its thing */
//callback is invoked - (passed in via args, make)
callback(xx)
}
function make(arg, callback){
//make funciton
}
//invoke it!
load('a', 'b', make);
Promise Example
承诺示例
loadas a JavaScript Promise
load作为 JavaScript Promise
var load = new Promise((resolve, reject) => {
// do whatever you require in here.
// when you are happy to fulfil, call resolve.
// if error, reject!
resolve(x); //or
reject(x);
})
// make function
function make(arg){
// do something
}
// use it!
load.then((x) => { make(x)}).catch((x) => {console.log(x)})
Async await with promise
异步等待与承诺
In this example the keyword await inside the async function makes javascript wait untill the promise is settled and returns us the result to be used to invoke the make()function.
在这个例子中,async 函数中的关键字 await 使 javascript 等待直到 promise 被解决并返回用于调用make()函数的结果。
async function load(){
// contents of the load funciton.
// call the make function when you have the outcome
// you expected. for example if you are getting data.
let getData = new Promise((resolve, reject) => {resolve('xx')})
let xx = await getData // wait till the getData promise is resolved.
make(xx);
}
回答by Eugen Govorun
You can use Promise object. It represents the eventual completion of an asynchronous operation, and its resulting value.
您可以使用 Promise 对象。它表示异步操作的最终完成及其结果值。
function first_letter() {
var bg_width = 5;
var bg_height = 10;
function load(xx, xy) {
return new Promise(resolve => {
// process your data
// for example
var result = xx * xy;
resolve(result);
});
}
function make(xxxxxx) {
console.log(xxxxxx);
}
load(bg_width, bg_height).then(result => make(result));
}

