简体   繁体   English

Node.js如何同步执行以下功能

[英]Node.js How to excecute the below function synchronously

I want to excecute the below function synchronously without change the time and output will be 123.How to do using node.js 我想在不更改时间的情况下同步执行以下功能,输出将是123.如何使用node.js

function f1(){
  setTimeout(function() {
    console.log('Hi I am order 1');
  }, 3000);
}

function f2() {
  setTimeout(function() {
    console.log('Hi I am order 2');
  }, 2000);
}

function f3() {
  setTimeout(function() {
    console.log('Hi I am order 3');
  }, 1000);
}

f3();
f2();
f1();

Your question seems incorrect, because of functions call order. 由于函数调用顺序,您的问题似乎不正确。 It looks like you want to call f1() first?. 看来您想先调用f1()?

In 2018 you can use ES6 with async/await: 在2018年,您可以将ES6与async / await一起使用:

 let delay = (time) => new Promise((resolve) => setTimeout(resolve, time)) async function f1(){ await delay(3000) console.log('Hi I am order 1') } async function f2() { await delay(2000) console.log('Hi I am order 2') } async function f3() { await delay(1000) console.log('Hi I am order 3'); } void async function run () { // It can be runned one-by-one await f1() await f2() await f3() // Or it can be runned in parallel // And then you lost ordering // await Promise.all([f3(), f2(), f1()]) } () 

A little bit of code refactoring 一点代码重构

function f1(){
    console.log('Hi I am order 1');
}

function f2() {
    console.log('Hi I am order 2');
}

function f3() {
    console.log('Hi I am order 3');
}

function delay(func, interval) {
    return new Promise(resolve => {
        setTimeout(() => {
            func();
            resolve();
        }, interval)
    })
}

(async () => {
    await delay(f1, 3000);
    await delay(f2, 2000);
    await delay(f3, 1000);
})();
  • Function f1 , f2 and f3 should only focus on what it does 函数f1f2f3应该只关注其功能
  • Use delay wrapper to delay the execution 使用delay包装器延迟执行
  • Use await to ensure sync execution 使用await来确保同步执行

JavaScript Promises are the better way to synchronize different functions, however an easier (though harder to maintain) way would be to add an argument to each function that executes whatever function you need to call next. JavaScript Promises是同步不同功能的更好方法,但是更简单(尽管更难维护)的方法是在每个函数中添加一个参数,以执行下一步需要调用的任何函数。

function f1(func) { setTimeout(function() { console.log('Hi I am order 1'); func(); }, 3000); }

function f2(func) { setTimeout(function() { console.log('Hi I am order 2'); func(); }, 2000); }

function f3(func) { setTimeout(function() { console.log('Hi I am order 3'); func(); }, 1000); }

f1(f2(f3)));

Another way would be to use the setTimeout function itself like so: 另一种方法是像这样使用setTimeout函数本身:

function f1() { setTimeout(function() { console.log('Hi I am order 1'); f2(); }, 3000); }

function f2() { setTimeout(function() { console.log('Hi I am order 2'); f3(); }, 2000); }

function f3() { setTimeout(function() { console.log('Hi I am order 3'); }, 1000); }

f1();

However, this method is less flexible if you want to do arbitrary nesting. 但是,如果要执行任意嵌套,则此方法的灵活性较差。

Edit: Fixed a mistake in my first example. 编辑:修复了我的第一个示例中的一个错误。

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

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