【问题标题】:Nodejs run task in sequenceNodejs按顺序运行任务
【发布时间】:2017-04-19 09:35:47
【问题描述】:

我是 node.js 的新手,我只是不知道如何在另一个函数之前执行 settimeout 函数,

例如,

var async = require('async');
function hello(){
    setTimeout(function(){
        console.log('hello');
    },2000);}

function world(){
    console.log("world");
}
async.series([hello,world()]);

输出总是世界你好。

我在使用图书馆吗?我不认为这个问题似乎微不足道,但我真的不知道如何强制一个短任务在一个长任务之后运行

【问题讨论】:

  • 几个问题:1)表达式world()立即执行world(这是在之前 hello被调用)2)hello返回 setTimeout 完成之前。要使用 async,需要使用 callback-idiom。请参阅stackoverflow.com/questions/15969082/… - 这解释了world() 的问题并显示了回调参数的使用。
  • 所以,不:您使用错误的库(并且没有正确解释行为)。按照文档搜索示例。

标签: node.js async.js


【解决方案1】:

您可以使用作为 nodejs 核心的回调。

var fun1 = function(cb){
 // long task
 // on done 
 return cb(null, result);
}

var fun2 = function(cb){
 return cb(null, data);
}

fun1(function(err, result){
 if(!err){
   fun2(function(er, data){
    // do here last task

   })
 }
}

// to avoid pyramid of doom use native promises

func1 (){
 return new Promise((resolve, reject) => {
   // do stuff here
   resolve(data)
 })

}
func2(){
  return new Promise((resolve, reject) => {
    resolve(data);
  })
}

然后调用它们:

func1.then((data) => {
 return func2();
})
.then((resule) => {
 //do here stuff when both promises are resolved
})

【讨论】:

  • 答案应该在async(它本身使用这样的回调)的上下文中,因为它已经给出了。这显示了末日之塔的基础,并没有解释原始代码的问题。
  • 好吧,你可以只使用承诺!您甚至不需要异步库来执行此操作,或者您也可以使用 yield 使您的代码更具可读性。
  • 谢谢大家!我也将更多地研究回调,因为我是 nodejs 的新手并且并不真正理解语法!
【解决方案2】:

Async 要求您使用callback。按照this 链接查看一些示例。以下代码应正确输出hello world

var async = require("async");
function hello(callback) {
    setTimeout(function(){
        console.log('hello');
        callback();
    }, 2000);
}

function world(callback) {
    console.log("world");
    callback();
}

async.series([hello, world], function (err, results) {
    // results is an array of the value returned from each function
    // Handling errors here
    if (err)    {
        console.log(err);
    }
});

请注意,callback() 是在 setTimeout() 函数内部调用的,因此它会等待 console.log('hello')

【讨论】:

    【解决方案3】:

    您可以使用承诺而不是回调。所以你的代码将如下所示:

      var async = require('async');
       function hello(){
       setTimeout(function(){
          console.log('hello');
         },2000);}
    
       function world(){
        console.log("world");
          }
        return Q.fcall(function() {
           hello();
        })
       .then(function(resultOfHelloFunction){
          world();
     });
    

    World() 函数只有在 hello() 函数执行完毕后才会执行。

    P.S : 我正在使用 Q 库来promisify 函数。使用其他库(例如 bluebird)来实现同样的事情完全没问题。

    【讨论】:

    • 既然他在问async,就给他一个关于承诺的答案没有多大意义。
    【解决方案4】:

    使用承诺

    function hello(){
        return new Promise(function(resolve, reject) {
            console.log('hello');
            resolve();
        });
    }
    
    function world(){
       return new Promise(function(resolve, reject) {
          console.log("world");
          resolve();
        });
    }
    
    
      hello()
      .then(function(){
         return world()
      })
      .then(function(){
        console.log('both done');
      })
      .catch(function(err){
         console.log(err);
      });
    

    【讨论】:

    • 如果我的答案对你有用,请你投票并标记为正确吗@ParosKwan
    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多