强制一个函数等待另一个函数完成
Forcing a function to wait until another function is complete
我是 JavaScript 的新手,已经尝试了一段时间但没有成功。
我有一个函数(任务 3)只能在函数完成之前执行。在它之前的函数(任务 1 和 2)中有更多的函数可以从其他来源获取数据,并且它们花费的时间是未知的。等待函数不会真正起作用,因为任务 1 和 2 的时间可能非常快或非常慢。我曾尝试进行 async/await 设置,但我一定做错了,因为它总是在任务 1 或 2 之前完成任务 3。与实际上没有回调的回调函数相同,它只是完成了任务 1 和 2,然后再也没有完成任务 3.
function task1(input){
// has more functions that do other stuff
}
function task2(input){
// has more functions that do other stuff
}
function task3(input){
// this code should only be executed after both task 1 and 2 finish
}
function main(input1, input2, input3){
task1(input1); // doesn't matter which task finishes first between 1 and 2
task2(input2);
task3(input3); // task 3 should not be executed until task 1 and 2 have completed.
}
main(input1, input2, input3);
如果有人能提供帮助,我们将不胜感激。
听起来您的环境支持 async/await。因此,假设 task1
和 task2
要么是 async
要么是 return 和 Promise
,那么:
- 使用
await Promise.all
等待两个任务完成。这不会强制执行任何命令,但会确保在继续之前都已完成。
- 呼叫
task3
.
async function task1(input){
// has more functions that do other stuff
}
async function task2(input){
// has more functions that do other stuff
}
function task3(input){
// this code should only be executed after both task 1 and 2 finish
}
async function main(input1, input2, input3){
await Promise.all(
task1(input1),
task2(input2)
)
task3(input3);
}
main(input1, input2, input3);
如果你有异步代码,你想使用承诺。 Promise.all() 将在 运行 之前等待它们全部完成。
function task1() {
return new Promise(function(resolve, reject) {
console.log("task 1")
setTimeout(function() {
resolve('foo');
}, Math.random() * 2000);
})
}
function task2() {
return new Promise(function(resolve, reject) {
console.log("task 2")
setTimeout(function() {
resolve('bar');
}, Math.random() * 2000);
})
}
function task3() {
console.log("task 3")
}
Promise.all([task1(), task2()]).then(function(values) {
console.log(values);
task3()
});
既然你说你正在使用 fetch,那么你可以使用它而不是 promise,因为那是 returns 一个 promise。
function task1() {
return fetch('http://example.com/foo.json')
.then(response => response.json())
}
function task2() {
return fetch('http://example.com/bar.json')
.then(response => response.json())
}
function task3() {
console.log("task 3")
}
Promise.all([task1(), task2()]).then(function(values) {
console.log(values);
task3()
});
由于您在任务中进行 API 调用,因此每个任务都应定义为异步函数,并按如下方式等待:
const fetch = require('node-fetch')
async function task1(input){
console.log(input)
// API call goes here
// const response = await fetch("http://localhost:5000/endpoint")
console.log(response)
}
async function task2(input){
console.log(input)
}
async function task3(input){
console.log(input)
}
async function main(input1, input2, input3){
await task1(input1); // doesn't matter which task finishes first between 1 and 2
await task2(input2);
await task3(input3); // task 3 should not be executed until task 1 and 2 have completed.
}
main("LIONS", "TIGERS", "BEARS")
您将在以下位置看到以上结果:
LIONS
TIGERS
BEARS
并且任何 API 调用将按顺序发生。
我是 JavaScript 的新手,已经尝试了一段时间但没有成功。
我有一个函数(任务 3)只能在函数完成之前执行。在它之前的函数(任务 1 和 2)中有更多的函数可以从其他来源获取数据,并且它们花费的时间是未知的。等待函数不会真正起作用,因为任务 1 和 2 的时间可能非常快或非常慢。我曾尝试进行 async/await 设置,但我一定做错了,因为它总是在任务 1 或 2 之前完成任务 3。与实际上没有回调的回调函数相同,它只是完成了任务 1 和 2,然后再也没有完成任务 3.
function task1(input){
// has more functions that do other stuff
}
function task2(input){
// has more functions that do other stuff
}
function task3(input){
// this code should only be executed after both task 1 and 2 finish
}
function main(input1, input2, input3){
task1(input1); // doesn't matter which task finishes first between 1 and 2
task2(input2);
task3(input3); // task 3 should not be executed until task 1 and 2 have completed.
}
main(input1, input2, input3);
如果有人能提供帮助,我们将不胜感激。
听起来您的环境支持 async/await。因此,假设 task1
和 task2
要么是 async
要么是 return 和 Promise
,那么:
- 使用
await Promise.all
等待两个任务完成。这不会强制执行任何命令,但会确保在继续之前都已完成。 - 呼叫
task3
.
async function task1(input){
// has more functions that do other stuff
}
async function task2(input){
// has more functions that do other stuff
}
function task3(input){
// this code should only be executed after both task 1 and 2 finish
}
async function main(input1, input2, input3){
await Promise.all(
task1(input1),
task2(input2)
)
task3(input3);
}
main(input1, input2, input3);
如果你有异步代码,你想使用承诺。 Promise.all() 将在 运行 之前等待它们全部完成。
function task1() {
return new Promise(function(resolve, reject) {
console.log("task 1")
setTimeout(function() {
resolve('foo');
}, Math.random() * 2000);
})
}
function task2() {
return new Promise(function(resolve, reject) {
console.log("task 2")
setTimeout(function() {
resolve('bar');
}, Math.random() * 2000);
})
}
function task3() {
console.log("task 3")
}
Promise.all([task1(), task2()]).then(function(values) {
console.log(values);
task3()
});
既然你说你正在使用 fetch,那么你可以使用它而不是 promise,因为那是 returns 一个 promise。
function task1() {
return fetch('http://example.com/foo.json')
.then(response => response.json())
}
function task2() {
return fetch('http://example.com/bar.json')
.then(response => response.json())
}
function task3() {
console.log("task 3")
}
Promise.all([task1(), task2()]).then(function(values) {
console.log(values);
task3()
});
由于您在任务中进行 API 调用,因此每个任务都应定义为异步函数,并按如下方式等待:
const fetch = require('node-fetch')
async function task1(input){
console.log(input)
// API call goes here
// const response = await fetch("http://localhost:5000/endpoint")
console.log(response)
}
async function task2(input){
console.log(input)
}
async function task3(input){
console.log(input)
}
async function main(input1, input2, input3){
await task1(input1); // doesn't matter which task finishes first between 1 and 2
await task2(input2);
await task3(input3); // task 3 should not be executed until task 1 and 2 have completed.
}
main("LIONS", "TIGERS", "BEARS")
您将在以下位置看到以上结果:
LIONS
TIGERS
BEARS
并且任何 API 调用将按顺序发生。