2016-10-18 7 views
0

これらの関数を1つずつ実行しようとしていますが、最初の関数を実行した後に2番目の関数に移動するなど、同時に2つの関数を同時にレンダリングします。 3000を超える機能がある場合、非常に多くのメモリを使用しています。Node.js - 1つずつ関数を実行する

webshot('google.com', 'google.jpeg', options, function(err) {}); 
webshot('yahoo.com', 'yahoo.jpeg', options, function(err) {}); 
+2

webshotは何ですか?あなたはそれらを順番に実行したいのですか、それとも他に何かがあります。 – Nivesh

+0

おそらく、このhttps://github.com/brenden/node-webshot – martinczerwi

+0

は、Googleのサムネイルを取って、それからヤフーの..それは同時に両方のサムネイルを撮っているタキンです。私はGoogleのサムネイルを撮りたいと思って、それと同時にヤフーのサムネイルを撮ってください。同じ時刻ではありません。 – user97811

答えて

2

これらのそれぞれの最後の引数は「コールバック」と呼ばれます。作業が終了したら呼び出されます。あなたは束これらのを持っている場合は(もちろん、

webshot('google.com', 'google.jpeg', options, function(err) { 
    webshot('yahoo.com', 'yahoo.jpeg', options, function(err) {}); 
}); 

あなたを:あなたが一度にこれらのいずれかを実行したいのであれば、前の1の次の1 内部のコールバックへの呼び出しを置きます言及3000!)、あなたはそれらのようにそれらを入れ子にするつもりはありません。私はおそらくあなたがそれらを渡す引数の配列を作成し、コールバックループ使用します。

function process(list, callback) { 
    var index = 0; 

    doOne(); 

    function doOne() { 
     var entry = list[index++]; 
     webshot(entry.domain, entry.img, entry.options, function(err) { 
      // ...error handling, etc... 
      if (index < list.length) { 
       doOne(); 
      } else { 
       callback(); 
      } 
     }); 
    } 
} 
var webshots = [ 
    {domain: 'google.com', img: 'google.jpeg', options: options}, 
    {domain: 'yahoo.com', img: 'yahoo.jpeg', options: options}, 
    // ... 
]; 
process(webshots, function() { 
    // All done 
}); 

サイドノート:これは約束で少しクリーナーだろうが。ノードスタイルのコールバックAPI(webshotのような)を約束する様々なライブラリがあります。あなたがした場合、あなたはこのようなものを約束を扱うことができる

var webshots = [ 
    {domain: 'google.com', img: 'google.jpeg', options: options}, 
    {domain: 'yahoo.com', img: 'yahoo.jpeg', options: options}, 
    // ... 
]; 
allDone = webshots.reduce(function(p, entry) { 
    return p.then(function() { 
     return promisifiedWebshot(entry.domain, entry.img, entry.options); 
    }); 
}, Promise.resolve()); 
allDone.then(function() { 
      // All done 
     }) 
     .catch(function() { 
      // Handle error 
     }); 
+1

あなたの魅力のように働いていただきありがとうございます! – user97811

1

あなたはasyncのような制御フローのライブラリを使用することができます。

'use strict'; 

const async = require('async'); 

async.series([ 
    (callback) => { 
     webshot('google.com', 'google.jpeg', options, callback); 
    }, 
    (callback) => { 
     webshot('yahoo.com', 'yahoo.jpeg', options, callback); 
    } 
], (error) => { 
    if(error) { 
     console.log('Error: ' + error); 
    } 
}); 

ユーティリティもありますが、mapeachように機能しますeachOfを使用すると、あなたのURLのリストを直接反復して、通話に適用することができます。

'use strict'; 

const async = require('async'), 
    urls = [ 
     {url: 'google.com', img: 'google.jpeg'}, 
     {url: 'yahoo.com', img: 'yahoo.jpeg'} 
    ]; 

async.map(urls, (url, callback) => { 
    webshot(url.url, url.img, options, callback); 
    //I presumed webshot call the callback like callback(error, result) 
}, (error, webshots) => { 
    //If a error occured, async will skip there and error will be set 
    //Else, webshots will be a array of all the results 
}); 
関連する問題